Flutter 混合模式下:saveLayer 混合注意点

直接Paint 绘制混合

js 复制代码
    canvas.saveLayer(Rect.fromLTWH(0, 0, width, height), Paint());
    Paint dstPaint = Paint()..color = Colors.red;
    dstPaint.strokeWidth = 20;
    dstPaint.style = PaintingStyle.stroke;
    canvas.drawImageRect(image!, Rect.fromLTWH(0, 0, image!.width.toDouble(), image!.height.toDouble()), Rect.fromLTWH(0, 0, width, height), dstPaint);

    var srcPaint = Paint()
      ..color = Colors.yellowAccent // 源颜色:蓝色
      ..style = PaintingStyle.fill // 填充模式
      ..blendMode = ui.BlendMode.clear
      ..strokeWidth = 70; // 混合模式
    canvas.drawRect(Rect.fromLTWH(0, 0, width, height).deflate(100), srcPaint);

效果图:

但是如果改成如下:

js 复制代码
    canvas.saveLayer(Rect.fromLTWH(0, 0, width, height), Paint());
    Paint dstPaint = Paint()..color = Colors.red;
    dstPaint.strokeWidth = 20;
    dstPaint.style = PaintingStyle.stroke;
    canvas.drawImageRect(image!, Rect.fromLTWH(0, 0, image!.width.toDouble(), image!.height.toDouble()), Rect.fromLTWH(0, 0, width, height), dstPaint);
    
    canvas.saveLayer(Rect.fromLTWH(0, 0, width/2, height/2), ui.Paint()
    ..blendMode = ui.BlendMode.clear);

    var srcPaint = Paint()
      ..color = Colors.yellowAccent // 源颜色:蓝色
      ..style = PaintingStyle.fill // 填充模式
      ..strokeWidth = 70; // 混合模式
    canvas.drawRect(Rect.fromLTWH(0, 0, width, height).deflate(100), srcPaint);

目测代码逻辑是一样的只是把混合模式设置给了Layer。但是最终效果就是界面上啥页没有全部被清空了。这是因为设置了混合模式之后,整个图层都会被当作src 进行混合。并且第一个设置范围的参数也不生效。

如果想限制到指定范围可以在saveLayer前加如下内容:

js 复制代码
  canvas.clipRect(Rect.fromLTWH(0, 0, width/2, height/2));

那么加完之后的效果图如下:

相关推荐
AiFlutter17 小时前
四、动画图表(03):饼图
flutter·低代码·低代码平台·aiflutter·aiflutter低代码
西西学代码17 小时前
Flutter---通过案例来详细了解状态管理
flutter
Mintopia17 小时前
🎙️ React Native(RN)语音输入场景全解析
android·react native·aigc
centor17 小时前
国际版 UnitySetup-Android-Support 安装 Mac 设备
android·macos
城东米粉儿17 小时前
compose 中的附带效应笔记一
android
LawrenceLan17 小时前
Flutter 零基础入门(八):Dart 类(Class)与对象(Object)
前端·flutter
前端不太难18 小时前
Flutter 列表性能的一套“长期安全写法”
安全·flutter·状态模式
STCNXPARM18 小时前
Android14显示系统 - VSYNC机制
android·surfaceflinger·vsync
say_fall18 小时前
C++ 类与对象易错点:初始化列表顺序 / 静态成员访问 / 隐式类型转换
android·java·开发语言·c++