網站首頁 編程語言 正文
CustomPaint 介紹
Flutter CustomPaint 提供了一個 canvas,可以在繪制階段在上面進行繪制內容。
需要繪制時,CustomPaint 首先要求它的 painter 在當前畫布上繪畫,然后它繪畫它的 child,在繪畫完它的 child 之后,要求他的 foregroundPainter 繪畫。
需要在從原點開始并包含給定大小的區域的矩形內作畫。 (如果在這些邊界之外繪畫,則分配的內存可能不足以光柵化繪畫命令,并且結果行為未定義。)要保證在這些邊界內繪畫,請考慮使用 ClipRect。
使用 CustomPaint
const CustomPaint({ super.key, this.painter, this.foregroundPainter, this.size = Size.zero, this.isComplex = false, this.willChange = false, super.child, })
最重要就是這個 painter,painter 需要自定義。
class MyPainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { var paint = Paint() ..color = Colors.red ..style = PaintingStyle.stroke ..strokeWidth = 1.0; canvas.drawLine( Offset(0, 10), Offset( 100, 10, ), paint); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) { return false; } }
需要重寫兩個方法,在 paint 方法中進行繪制。如果繪制不受外界影響,shouldRepaint 返回 false 就好。
CustomPaint(painter: MyPainter(),);
把 MyPainter 賦值給參數 painter 就好了。
size 的大小。
如果 CustomPaint 的約束可以為 0 的話,那么 size 的值 Size(0,0)
,就是說,size 的值總是 Constrains 的最小 width,最小 height。有兩種辦法可以改變 size。
- 可以給 CustomPaint 加上約束,比如加一個 SizedBox
SizedBox( height: 20, width: 20, child: CustomPaint( painter: MyPainter(), ))
- 可以直接指定 size 參數。
SizedBox( height: 20, width: 20, child: CustomPaint( size:Size(30,30), painter: MyPainter(), ))
size 參數可以在 constrains 的范圍內起到作用。在本例中,約束為 20, size 為 30,最后傳給 paint 方法的 size 為 20。 tight 約束下 size 參數無效,只有 在loose 約束下 ,size 參數才能起到作用。
isComplex
是否提示應該緩存該層的繪畫。如果 為false,則合成器將自己來決定這一層是否應該緩存。
willChange
是否應該告知緩存層這幅畫在下一幀中可能會發生變化。如果 isComplex 為 true,才需要考慮這個參數。
foregroundPainter
默認繪制的層是在 child 之下,foregroundPainter 在 child 之上。
動畫
CustomPainter 有一個 可選的參數 Listenable? repaint
,是用來做動畫的。
舉個例子。
class MyPainter extends CustomPainter { MyPainter(Animation<double> animation) :_animation=animation, super(repaint: animation); final Animation<double> _animation; @override void paint(Canvas canvas, Size size) { var paint = Paint() ..color = Colors.red ..style = PaintingStyle.stroke ..strokeWidth = 1.0; canvas.drawLine( Offset(0, 10), Offset( 100*_animation.value, 10, ), paint); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) { return false; } } class MyStatefulWidget extends StatefulWidget { const MyStatefulWidget({super.key}); @override State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); } class _MyStatefulWidgetState extends State<MyStatefulWidget> with SingleTickerProviderStateMixin{ late AnimationController controller= AnimationController(duration: Duration(seconds: 2),vsync: this)..repeat(); @override Widget build(BuildContext context) { return SizedBox( height: 20, width: 20, child: CustomPaint( painter: MyPainter(controller.view), )); } }
會看到一條紅色的直線由短變長。
原文鏈接:https://juejin.cn/post/7175666604963790885
相關推薦
- 2022-10-11 Windows下vs中對DLL、exe文件添加屬性信息
- 2023-08-13 Fastadmin后臺頁面添加頂部按鈕
- 2022-03-14 Maven項目中遇見的一些問題(maven項目報錯)
- 2022-12-10 C語言中使用qsort函數對自定義結構體數組進行排序_C 語言
- 2022-11-09 Android開發實現圖片的上傳下載_Android
- 2023-01-17 pytorch?geometric的GNN、GCN的節點分類方式_python
- 2022-03-16 swift?cell自定義左滑手勢處理方法_Swift
- 2022-05-22 Nginx的基本概念和原理_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支