0

我试图使用 alpha 通道在带有CustomPaint's 的线条颜色上应用透明度Canvas.drawLine()

问题

但是,alpha 通道上的颜色调整对结果没有影响,例如,下面的代码仍然给我 100% 不透明的白线


  final gridPaint = Paint()
    ..strokeJoin = StrokeJoin.miter
    ..strokeWidth = 1.0
    ..color = const Color(0xe6ffffff)
    ..style = PaintingStyle.stroke;


  canvas.drawLine(start, end, gridPaint);

解决方法

我必须绘制 1 像素宽drawRect才能获得透明线条。

问题

这是设计使然吗?

4

2 回答 2

1

您可以对您选择的颜色使用withOpacity方法。

Colors.green.withOpacity(0.5)

或者

Color(0xe6ffffff).withOpacity(0.5)

根据您希望达到的不透明度级别,将该值设置为 0 到 1 之间的数字。

于 2020-07-31T07:40:08.837 回答
0

我代表@pskink 从我的 Android 设备上发布他的测试代码和生成的图像:

    print('devicePixelRatio: ' + MediaQuery.of(context).devicePixelRatio.toString());
    var pr = ui.PictureRecorder();
    var c = Canvas(pr);
    var p = Paint();
    c.drawRect(Rect.fromLTWH(0, 0, 1, 3), p..color = Color(0x8000ff00));
    c.drawRect(Rect.fromLTWH(1.5, 0, 1, 3), p);
    c.drawLine(Offset(3.5, 3.0), Offset(3.5, 6.0), p..color = Color(0x80ff0000));
    c.drawLine(Offset(5.0, 3.0), Offset(5.0, 6.0), p);
    var img = await pr.endRecording().toImage(6, 6);
    var data = await img.toByteData(format: ui.ImageByteFormat.png);
    final imgDir = Platform.isAndroid ?
    (await pathman.getExternalStorageDirectories(type: pathman.StorageDirectory.pictures))[0] :
    (await pathman.getApplicationDocumentsDirectory());
    if (Platform.isAndroid) {
      await Directory(imgDir.path).create(recursive: true);
    }
    final suffix = DateTime.now().toString().replaceAll(RegExp(r'[\:\ \.]+'), '-');
    final filePath = '${imgDir.path}/screenshot-$suffix.png';
    File(filePath).writeAsBytesSync(data.buffer.asInt8List());

在我CustomPaint的 's中有上面的代码paint()给了我下面的小图像。

在此处输入图像描述

于 2020-08-19T06:38:48.483 回答