当前位置:网站首页>Paint basic graphics with custompaint
Paint basic graphics with custompaint
2022-07-07 22:00:00 【Ma Nong @ official account on the island with the same name】
Last one First time to know Flutter Drawing components for — CustomPaint We introduced CustomPaint
The basic concept and use of , You can see CustomPaint
Actually sum Front-end Canvas
It's basically the same , Actually the front end Canvas
Supported drawing methods CustomPaint
All support , After all CustomPaint
It's also based on Canvas
Realized . In this article we will introduce CustomPaint
Drawing basic graphics .
Draw a rectangle
Drawing a rectangle is relatively simple , The method is defined as follows :
void drawRect(Rect rect, Paint paint)
among rect
To draw a rectangle ,paint
That is, the brush configuration object . For example, we want to draw a width horizontally in the middle 200 x 120 The rectangular , You can use the following code :
canvas.drawColor(Color(0xFFF1F1F1), BlendMode.color);
var center = size / 2;
// Draw a rectangle
var paint = Paint()..color = Color(0xFF2080E5);
paint.strokeWidth = 2.0;
canvas.drawRect(
Rect.fromLTWH(center.width - 100, 60, 200, 120),
paint,
);
paint
The default is filled with solid , If you want to idle fill , Set up paint
Object's style
attribute by PaintingStyle.stroke
that will do .
Draw a circle
Drawing a circle is described in the previous article , Similar to drawing a rectangle , The only parameter passed in is the center position , Radius and paint
object .
canvas.drawCircle(
Offset(center.width - 80, 240),
40,
paint,
);
Draw the ellipse
The size of the ellipse is constrained by an circumscribed rectangle , The call form is the same as drawing a rectangle .
canvas.drawOval(
Rect.fromLTWH(center.width, 200, 120, 80),
paint,
);
Draw any shape
Draw any shape drawPath
Realization , The shape path to be drawn Use Path
Just build the object . Take drawing an equilateral triangle as an example , After we determine the positions of the three vertices , Use Path
Object's lineTo
Method to connect three vertices . Here is the implementation code :
// Use Path Draw triangle
Path trianglePath = Path();
// Hollow drawing
paint.style = PaintingStyle.stroke;
trianglePath.moveTo(center.width - 30, 300);
trianglePath.lineTo(center.width + 30, 300);
trianglePath.lineTo(center.width, 352);
trianglePath.lineTo(center.width - 30, 300);
canvas.drawPath(trianglePath, paint);
draw arc
Drawing an arc is actually realized by drawing an ellipse , Just control the range of arc drawing by controlling the angle of drawing . The method of drawing an arc is defined as follows :
void drawArc(Rect rect, double startAngle, double sweepAngle, bool useCenter, Paint paint)
among rect
It's a constrained rectangle of elliptical size ,startAngle
Is the starting angle ,sweepAngle
Is the angle range drawn .useCenter
Indicates whether to close to the center point of the rectangle , The default is true
. That is, clockwise . The default is to connect the straight line from the center point to the point corresponding to the starting angle, and then connect the straight line to the center point after drawing the specified angle range . If useCenter
by false
, Then close it directly according to the starting and ending points of the arc , Instead of going back to the center . Here are the differences between the two kinds of drawing , On the left is useCenter
by false
, On the right is useCenter
by true
, Other parameters are the same . The following code is an example code for drawing an arc :
canvas.drawArc(
Rect.fromLTWH(center.width - 60, 340, 120, 80),
0,
pi / 2,
false,
paint,
);
summary
This article introduces CustomPaint Methods and examples of drawing basic graphics , actually Flutter Of Canvas There are many other ways to draw graphics , Such as drawing lines , Draw a rounded rectangle , Draw text and so on , If you are interested, you can refer to the instruction document on the official website to see the use of each method . With the foundation of drawing basic graphics , We can draw some interesting graphics —— UI The task assigned by my little sister has not been completed yet !
边栏推荐
- Reinforcement learning - learning notes 8 | Q-learning
- npm uninstall和rm直接删除的区别
- Default constraint and zero fill constraint of MySQL constraint
- Contour layout of margin
- ByteDance senior engineer interview, easy to get started, fluent
- Main functions of OS, Sys and random Standard Libraries
- Hdu4876zcc love cards (multi check questions)
- [开源] .Net ORM 访问 Firebird 数据库
- Take the intersection of two sets
- Feature generation
猜你喜欢
L2:ZK-Rollup的现状,前景和痛点
EasyCVR配置中心录像计划页面调整分辨率时的显示优化
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
Using enumeration to realize English to braille
An in-depth understanding of fp/fn/precision/recall
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
648. Word replacement
Jerry's test box configuration channel [chapter]
L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
Default constraint and zero fill constraint of MySQL constraint
随机推荐
谈谈制造企业如何制定敏捷的数字化转型策略
Qt编写物联网管理平台39-报警联动
The little money made by the program ape is a P!
Implementation of mahout Pearson correlation
ByteDance Android interview, summary of knowledge points + analysis of interview questions
Arlo's troubles
Restapi version control strategy [eolink translation]
Win11时间怎么显示星期几?Win11怎么显示今天周几?
Open source OA development platform: contract management user manual
Using enumeration to realize English to braille
201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
Codemail auto collation code of visual studio plug-in
Devil daddy B1 hearing the last barrier, break through with all his strength
The cyberspace office announced the measures for data exit security assessment, which will come into force on September 1
Google SEO external chain backlinks research tool recommendation
强化学习-学习笔记9 | Multi-Step-TD-Target
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
[open source] Net ORM accessing Firebird database
L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?