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

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

相关推荐
mmsx2 小时前
Android APK 加固方案怎么落地:从加壳到校验的组合取舍
android·网络·安全·签名
hunterandroid3 小时前
Navigation Component 工程落地:从单 Activity 架构到深度链接配置
android·前端
撩得Android一次心动3 小时前
Jetpack Compose 知识点整理3【个人用】
android·笔记·学习·kotlin·android compose
Kapaseker3 小时前
别再只会写 <T>:Kotlin 泛型的实战用法
android·kotlin
FlightYe4 小时前
音视频修炼之容器与协议(一):MP4和MOV的box剖析
android·linux·c++·ffmpeg·音视频
Vitaliy Kostrov(柯维远)6 小时前
客户端如何检查直播流:从流内标记到停止播放
android·运维·服务器·驱动开发·网络安全·系统架构·cdn
恋猫de小郭6 小时前
Dart 3.15 新特性:this 终于支持类型提升了
android·前端·flutter
ii_best7 小时前
ios手机开发安卓脚本开发软件[按键精灵教程]从「找最小值」到「Boss 最近刷新」再到「屏幕范围极值」
android·ios·智能手机·自动化·按键精灵
开开心心就好7 小时前
安卓PDF处理工具,免费无广告零弹窗
android·服务器·开发语言·算法·pdf·ocr
墨狂之逸才7 小时前
扫码枪响了,应用却没收到:一次 Android USB 线材问题排查
android