- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在阅读有关 Flutter 的文章,并了解到可以使用 CustomPainter 和 CustomClipper 创建形状。矩形、椭圆形等形状可以使用两者创建,那么它们之间有什么区别以及何时应该使用它们中的任何一个?
最佳答案
裁剪器用于将渲染限制在特定区域。渲染引擎只会“绘制”定义区域内的像素。 CustomClipper
将主要与 ClipPath
一起使用小部件。
class MyCustomClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
// Implement your custom clipper path
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
ClipPath(
clipper: MyCustomClipper(),
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
);
此接口(interface)与 CustomPaint
一起使用它提供了一个 Canvas ,您可以在上面绘制图像和形状。
class MyCustomPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// Implement your custom painter
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
CustomPaint(
painter: MyCustomPainter(),
);
继续:CustomClipper
正在限制绘画区域,同时CustomPainter
给你一 block Canvas ,让你自由作画。
虽然根据您的用例,您可能能够通过两者获得相似的结果,但使用 CustomClipper
可能更容易或 CustomPainter
.
例如,如果您只想为 Container
设置一些自定义形状或 Image
使用 CustomClipper
会容易得多与 CustomPainter
相比因为您只需要定义小部件将在其中呈现的路径,您仍然可以访问小部件的所有属性。
现在让我们再举一个例子,如果你想创建自己的CircularProgressIndicator
小部件。在这种情况下,您可能很难使用 CustomClipper
同时感谢CustomPainter
你将能够更自由地绘制你的小部件,它也将更容易制作动画。 (如果您查看 ProgressIndicator
的编码方式,您会发现它们使用的是 CustomPainter
)
关于flutter - CustomPainter 和 CustomClipper 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55704331/
我有一个 Container ClipPath 内的小部件它使用 CustomClipper .一切正常,我有所需的小部件形状。 但是,我找不到为这个自定义形状的 Widget 制作阴影的方法。 另外
我目前正在阅读有关 Flutter 的文章,并了解到可以使用 CustomPainter 和 CustomClipper 创建形状。矩形、椭圆形等形状可以使用两者创建,那么它们之间有什么区别以及何时应
我正在尝试通过使用 Material 的海拔高度在自定义路径上添加海拔高度,但看起来并没有这样做任何东西,我都尝试过其他方法,但在整个 Material (矩形)上得到了提升。 import 'pac
我想知道 ShapeBorderClipper 类和 CustomClipper 类之间的区别是什么,什么时候使用它们最好 最佳答案 ShapeBorderClipper 是一种特殊类型的 Custo
我是一名优秀的程序员,十分优秀!