gpt4 book ai didi

c++ - flutter :即使 shouldRepaint() 返回 true,自定义画家也不会重新绘制

转载 作者:行者123 更新时间:2023-12-03 04:20:36 30 4
gpt4 key购买 nike

我正在通过从 C/C++ 端获取颜色数据并使用 CustomPainter 绘制屏幕来测试 flutter FFI .
但令我惊讶的是,即使我将画家设置为始终重画,paint()函数只被调用两次。
代码

class _ColorViewPainter extends CustomPainter {
Color clrBackground;

_ColorViewPainter({
this.clrBackground = Colors.black
});

@override
bool shouldRepaint(_ColorViewPainter old) => true;

@override
void paint(Canvas canvas, Size size) {
print("paint: start");

final color = ffiGetColor().ref;

final r = color.r;
final g = color.g;
final b = color.b;

print("color: $r, $g, $b");

final paint = Paint()
..strokeJoin = StrokeJoin.round
..strokeWidth = 1.0
..color = Color.fromARGB(255, r, g, b)
..style = PaintingStyle.fill;

final width = size.width;
final height = size.height;
final content = Offset(0.0, 0.0) & Size(width, height);
canvas.drawRect(content, paint);

print("paint: end");
}
}
ffiGetColor()函数只是从 C/C++ 端检索颜色 RGB 结构。我可以看到屏幕被更新了两次,日志显示:
I/flutter (12096): paint: start
I/flutter (12096): color: 255, 0, 0
I/flutter (12096): paint: end
I/flutter (12096): paint: start
I/flutter (12096): color: 0, 255, 0
I/flutter (12096): paint: end
I/Surface (12096): opservice is null false
但就是这样。尽管我显然希望它用 shouldRepaint 重新绘制. flutter 没有这样做。
怎么了?
这是我的环境
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel dev, v1.18.0-dev.5.0, on Mac OS X 10.15.5 19F101, locale en-CA)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
[✓] Android Studio (version 4.0)
[✗] Cannot determine if IntelliJ is installed
✗ Directory listing failed
[✓] VS Code (version 1.44.2)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

最佳答案

感谢@pskink 的提示,我设法让我的 CustomPainter 不断地重新绘制,尽管它仍然不完美。
dart/flutter: CustomPaint updates at a lower rate than ValueNotifier's value update
因为当前的解决方案仍然存在更新率问题,所以我只是简单地报告一下我所得到的:
我基本上必须构造一个通知器并通过轮询在适当的位置为其分配 ffi 检索到的值。


// in initState() of State class
_notifier = ValueNotifier<NativeColor>(ffiGetColor().ref);

...

// in a timer method
_notifier.value = ffiGetColor().ref;
然后用 CustomPaint , 绑定(bind)通知器以在其构造函数中重绘
  @override
Widget build(BuildContext context) {
return Container(

...


child: CustomPaint(
painter: _ColorViewPainter(
context: context,
notifier: _notifier,
...
)
)
);
}

class _ColorViewPainter extends CustomPainter {
ValueNotifier<NativeColor> notifier;
BuildContext context;
Color clrBackground;

_ColorViewPainter({this.context, this.notifier, this.clrBackground})
: super(repaint: notifier) {
}

@override
bool shouldRepaint(_ColorViewPainter old) {
print('should repaint');
return true;
}

@override
void paint(Canvas canvas, Size size) {
print("paint: start");
final r = notifier.value.r;
final g = notifier.value.g;
final b = notifier.value.b;
print("color: $r, $g, $b");
final paint = Paint()
..strokeJoin = StrokeJoin.round
..strokeWidth = 1.0
..color = Color.fromARGB(255, r, g, b)
..style = PaintingStyle.fill;

final width = size.width;
final height = size.height;
final content = Offset(0.0, 0.0) & Size(width, height);
canvas.drawRect(content, paint);
print("paint: end");
}

}
问题 ValueNotifier依赖于以下条件才能向其监听器发出通知
  • 它绑定(bind)的值类型定义了 operator ==
  • ValueNotifier的值字段,您选择的数据对象绑定(bind)到该字段,被明确重新分配 带有关于更改的新数据对象。

  • 当我们将通知程序绑定(bind)到自定义类时,这一点至关重要。使用自定义类,我们必须
  • 重载其operator == .
  • 将新对象分配给 ValueNotifier<MyClass>.value而不是通过调用自己的常规方法来修改数据对象的值。

  • 否则 CustomPaintpaint()不会在所需的更改上调用。
    举个例子,这个自定义类没有资格与 ValueNotifier 绑定(bind)。 , 因为没有重载 operator== :
    class MyClass {
    int prop = 0;

    void changeValue(newValue) {
    prop = newValue;
    }

    }
    再举一个例子,假设我们有一个自定义类:
    class MyClass {
    int prop = 0;

    @override
    bool operator ==(covariant MyClass other) {
    return other is MyClass && prop != other. prop;
    }

    void changeValue(newValue) {
    prop = newValue;
    }

    }
    这将起作用:

    class _MyViewState extends State<MyView> {
    ValueNotifier<MyClass> notifier;
    Timer _timer;

    ....

    @override
    initState() {
    super.initState();

    notifier = ValueNotifier<MyClass>(MyClass());
    _timer = Timer.periodic(Duration(milliseconds: 10), _updateData);
    }

    _updateData(Timer t) {
    var myObj = MyClass(newValue);
    notifier.value = myObj;
    }

    }
    但这行不通。
    class _MyViewState extends State<MyView> {
    ValueNotifier<MyClass> notifier;
    Timer _timer;

    ....

    @override
    initState() {
    super.initState();

    notifier = ValueNotifier<MyClass>(MyClass());
    _timer = Timer.periodic(Duration(milliseconds: 10), _updateData);
    }


    _updateData(Timer t) {
    var newValue = getNewValueSomewhere();
    notifier.value.changeValue(newValue);
    }


    关于c++ - flutter :即使 shouldRepaint() 返回 true,自定义画家也不会重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62849607/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com