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));

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

相关推荐
消失的旧时光-19433 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
Jinkxs3 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&3 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
LDORntKQH3 小时前
基于深度强化学习的混合动力汽车能量管理策略 1.利用DQN算法控制电池和发动机发电机组的功率分配 2
android
冬奇Lab4 小时前
Android 15 ServiceManager与Binder服务注册深度解析
android·源码·源码阅读
子春一4 小时前
Flutter for OpenHarmony:色彩捕手:基于 CIELAB 色差模型与人眼感知的高保真色彩匹配游戏架构解析
flutter·游戏·架构
ZH15455891315 小时前
Flutter for OpenHarmony Python学习助手实战:数据库操作与管理的实现
python·学习·flutter
Lionel6895 小时前
Flutter 鸿蒙:获取真实轮播图API数据
flutter
2501_916008895 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
千逐686 小时前
《基于 Flutter for OpenHarmony 的沉浸式天气可视化系统设计与实现》
flutter