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

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

相关推荐
向哆哆8 小时前
用 Flutter × OpenHarmony 构建智能健康提醒应用:健康档案管理实战
flutter·开源·鸿蒙·openharmony·开源鸿蒙
泡泡以安8 小时前
Android 逆向实战:从零突破某电商 App 登录接口全参数加密
android·爬虫·安卓逆向
菜鸟小芯9 小时前
【开源鸿蒙跨平台开发先锋训练营】DAY8~DAY13 底部选项卡&动态功能实现
flutter·harmonyos
向哆哆9 小时前
Flutter × OpenHarmony 实战 | 打造画栈平台的顶部横幅组件
flutter·开源·鸿蒙·openharmony·开源鸿蒙
2501_944525549 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 预算详情页面
android·开发语言·前端·javascript·flutter·ecmascript
雨季6669 小时前
Flutter 三端应用实战:OpenHarmony 简易“动态主题切换卡片”交互模式
flutter·ui·交互·dart
向哆哆10 小时前
构建健康档案管理快速入口:Flutter × OpenHarmony 跨端开发实战
flutter·开源·鸿蒙·openharmony·开源鸿蒙
清蒸鳜鱼10 小时前
【Mobile Agent——Droidrun】MacOS+Android配置、使用指南
android·macos·mobileagent
mocoding10 小时前
使用Flutter强大的图标库fl_chart优化鸿蒙版天气预报温度、降水量、湿度展示
flutter·华为·harmonyos
2501_9159184110 小时前
HTTPS 代理失效,启用双向认证(mTLS)的 iOS 应用网络怎么抓包调试
android·网络·ios·小程序·https·uni-app·iphone