Flutter 34: 图解自定义 View 之 Canvas (一)
??????小菜最近在學(xué)習(xí)自定義 View,剛了解了一下 Paint 畫筆的神奇之處,現(xiàn)在學(xué)習(xí)一下 Canvas 畫布的神秘之處。Flutter 提供了眾多的繪制方法,小菜接觸不深,盡量都嘗試一下。
Canvas 畫布
drawColor 繪制背景色
??????drawColor 需要傳入兩個參數(shù),第一個為色值,第二個為混合模式,有眾多混合模式供選擇,但注意使用混合模式后會與繪制其上的其他 View 顏色混合像素。
canvas.drawColor(Colors.pinkAccent, BlendMode.srcIn);drawPoints 繪制點/線
??????drawPoints 不僅可以繪制點,還可以繪制點與點的連線;PointMode 包括 points 點 / lines 線 / polygon 多邊形;注意 lines 為每兩點之間的連線,若為奇數(shù)個點,最后一個沒有與之相連的點。
// 繪制點 canvas.drawPoints(PointMode.points,[Offset(30.0, 30.0), Offset(60.0, 30.0),Offset(90.0, 30.0), Offset(90.0, 60.0),Offset(60.0, 60.0), Offset(30.0, 60.0)],Paint()..strokeWidth = 4.0); canvas.drawPoints(PointMode.points,[Offset(160.0, 30.0), Offset(190.0, 30.0),Offset(220.0, 30.0), Offset(220.0, 60.0),Offset(190.0, 60.0), Offset(160.0, 60.0)],Paint()..strokeWidth = 4.0..strokeCap = StrokeCap.round); // 繪制線 canvas.drawPoints(PointMode.lines,[Offset(30.0, 100.0), Offset(60.0, 100.0),Offset(90.0, 100.0), Offset(90.0, 130.0),Offset(60.0, 130.0), Offset(30.0, 130.0)],Paint()..strokeWidth = 4.0..strokeCap = StrokeCap.round); // 繪制多邊形 canvas.drawPoints(PointMode.polygon,[Offset(160.0, 100.0), Offset(190.0, 100.0),Offset(220.0, 100.0), Offset(220.0, 130.0),Offset(190.0, 130.0), Offset(160.0, 130.0)],Paint()..strokeWidth = 4.0..strokeCap = StrokeCap.round);drawLine 繪制線
canvas.drawLine(Offset(30.0, 90.0), Offset(Screen.width - 30.0, 90.0),Paint()..strokeWidth = 4.0); canvas.drawLine(Offset(30.0, 120.0), Offset(Screen.width - 30.0, 120.0),Paint()..strokeWidth = 4.0..strokeCap = StrokeCap.round); canvas.drawLine(Offset(30.0, 150.0), Offset(Screen.width - 30.0, 150.0),Paint()..strokeWidth = 4.0..strokeCap = StrokeCap.square);drawArc 繪制弧/餅
??????drawArc 可以用來繪制圓弧甚至配合 Paint 繪制餅狀圖;drawArc 的第一個參數(shù)為矩形范圍,即圓弧所在的圓的范圍,若非正方形則圓弧所在的圓會拉伸;第二個參數(shù)為起始角度,0.0 為坐標(biāo)系 x 軸正向方形;第三個參數(shù)為終止角度,若超過 2*PI,則為一個圓;第四個參數(shù)為是否由中心出發(fā),false 時只繪制圓弧,true 時繪制圓餅;第五個參數(shù)即 Paint 畫筆,可通過 PaintingStyle 屬性繪制是否填充等;
const PI = 3.1415926; canvas.drawArc(Rect.fromCircle(center: Offset(60.0, 60.0), radius: 80.0),0.0, PI / 2, false,Paint()..color = Colors.white..strokeCap = StrokeCap.round..strokeWidth = 4.0..style = PaintingStyle.stroke); canvas.drawArc(Rect.fromCircle(center: Offset(200.0, 60.0), radius: 80.0),0.0, PI / 2, false,Paint()..color = Colors.white..strokeWidth = 4.0..style = PaintingStyle.fill); canvas.drawArc(Rect.fromCircle(center: Offset(90.0, 160.0), radius: 80.0),0.0, PI * 2 / 3, true,Paint()..color = Colors.white..strokeWidth = 4.0..style = PaintingStyle.stroke); canvas.drawArc(Rect.fromCircle(center: Offset(250.0, 160.0), radius: 80.0),0.0, PI * 2 / 3, true,Paint()..color = Colors.white..strokeWidth = 4.0..style = PaintingStyle.fill); canvas.drawArc(Rect.fromLTWH(30.0, 300.0, 200.0, 100.0),0.0, 5.0, true,Paint()..color = Colors.white..style = PaintingStyle.fill); canvas.drawArc(Rect.fromPoints(Offset(260.0, 260.0), Offset(320.0, 420.0)),0.0, 5.0, true,Paint()..color = Colors.white..style = PaintingStyle.fill);drawRect 繪制矩形
??????drawRect 用來繪制矩形,Flutter 提供了多種繪制矩形方法:
drawRRect 繪制圓角矩形
??????drawRRect 繪制圓角矩形,Flutter 提供了多種繪制方法:
drawDRRect 繪制嵌套矩形
??????drawDRRect 繪制嵌套矩形,第一個參數(shù)為外部矩形,第二個參數(shù)為內(nèi)部矩形,可用上述多種設(shè)置圓角矩形方式;最后一個參數(shù)為 Paint 畫筆,且 PaintingStyle 為 fill 時填充的是兩個矩形之間的范圍。
canvas.drawDRRect(RRect.fromRectXY(Rect.fromCircle(center: Offset(90.0, 420.0), radius: 60.0), 8.0, 8.0),RRect.fromRectXY(Rect.fromCircle(center: Offset(90.0, 420.0), radius: 54.0), 8.0, 8.0),Paint()..color = Colors.whit..strokeWidth = 3.0..style = PaintingStyle.stroke); canvas.drawDRRect(RRect.fromRectXY(Rect.fromCircle(center: Offset(270.0, 420.0), radius: 60.0), 8.0, 8.0),RRect.fromRectXY(Rect.fromCircle(center: Offset(270.0, 420.0), radius: 54.0), 8.0, 8.0),Paint()..color = Colors.white..strokeWidth = 3.0..style = PaintingStyle.fill);drawCircle 繪制圓形
??????drawCircle 繪制圓形,僅需設(shè)置原點及半徑即可;
canvas.drawCircle(Offset(90.0, 420.0), 60.0,Paint()..color = Colors.white..strokeWidth = 3.0..style = PaintingStyle.stroke); canvas.drawCircle(Offset(270.0, 420.0), 60.0,Paint()..color = Colors.white..strokeWidth = 3.0..style = PaintingStyle.fill);drawOval 繪制橢圓
??????drawOval 繪制橢圓方式很簡單,主要繪制一個矩形即可;
canvas.drawOval(Rect.fromLTRB(30.0, 30.0, 150.0, 100.0),Paint()..color = Colors.white..strokeWidth = 3.0..style = PaintingStyle.stroke); canvas.drawOval(Rect.fromLTRB(210.0, 30.0, 330.0, 100.0),Paint()..color = Colors.white..strokeWidth = 3.0..style = PaintingStyle.fill);drawPath 繪制路徑
??????drawPath 用來繪制路徑,Flutter 提供了眾多路徑方法,小菜嘗試幾種常用的方法:
??????小菜繪制了一個基本的坐標(biāo)系來比較一下 moveTo()/lineTo() 與 relativeMoveTo()/relativeLineTo() 的區(qū)別:
canvas.drawPath(Path()..relativeMoveTo(30.0, 30.0)..relativeLineTo(120.0, 30.0)..relativeLineTo(90.0, 60.0)..relativeLineTo(180.0, 60.0),Paint()..color = Colors.blue..strokeWidth = 6.0..style = PaintingStyle.stroke); canvas.drawPath(Path()..moveTo(30.0, 30.0)..lineTo(120.0, 30.0)..lineTo(90.0, 60.0)..lineTo(180.0, 60.0),Paint()..color = Colors.orange..strokeWidth = 6.0..style = PaintingStyle.stroke);??????小菜對自定義 View 研究還不深入,有很多方法還沒有嘗試,有錯誤的地方希望多多指導(dǎo)!
總結(jié)
以上是生活随笔為你收集整理的Flutter 34: 图解自定义 View 之 Canvas (一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页登陆验证之图片验证码
- 下一篇: python内置函数中的zip,max,