当前位置:网站首页>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 !
边栏推荐
- Open source OA development platform: contract management user manual
- How to turn on win11 game mode? How to turn on game mode in win11
- QT compile IOT management platform 39 alarm linkage
- Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
- Default constraint and zero fill constraint of MySQL constraint
- Reptile combat (VII): pictures of the king of reptiles' heroes
- Demon daddy B2 breaks through grammar and completes orthodox oral practice
- NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
- Deadlock conditions and preventive treatment [easy to understand]
- Reinforcement learning - learning notes 9 | multi step TD target
猜你喜欢
Ten thousand word summary data storage, three knowledge points
An overview of the latest research progress of "efficient deep segmentation of labels" at Shanghai Jiaotong University, which comprehensively expounds the deep segmentation methods of unsupervised, ro
Debugging and handling the problem of jamming for about 30s during SSH login
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Jerry's about TWS channel configuration [chapter]
TCP/IP 协议栈
Redis - basic use (key, string, list, set, Zset, hash, geo, bitmap, hyperloglog, transaction)
Jerry's about TWS pairing mode configuration [chapter]
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
强化学习-学习笔记9 | Multi-Step-TD-Target
随机推荐
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
JNI primary contact
ISO 26262 - considerations other than requirements based testing
DNS series (I): why does the updated DNS record not take effect?
Codemail auto collation code of visual studio plug-in
The function is really powerful!
[JDBC Part 1] overview, get connection, CRUD
三元表达式、各生成式、匿名函数
Embedded development: how to choose the right RTOS for the project?
Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
Node:504 error reporting
Tsconfig of typescript TS basics JSON configuration options
Which futures company is the safest to open a futures account?
Using enumeration to realize English to braille
How to turn on win11 game mode? How to turn on game mode in win11
建立自己的网站(18)
Backup tidb cluster to persistent volume
Win11如何解禁键盘?Win11解禁键盘的方法
Can I open a stock account directly online now? Is it safe?
How much does it cost to develop a small program mall?