Summary of Common Methods of Android Drawing
- 2021-07-10 20:47:08
- OfStack
What are the common methods of Android drawing? The following 11 are listed for everyone:
1. Methods about brushes (Paint)
Paint mPaint= new Paint();
mPaint.setAntiAlias(true); // Anti-aliasing
mPaint.setStrokeWidth(mCircleWidth); // Set the width of the ring
mPaint.setStrokeCap(Paint.Cap.ROUND); // Define the shape of line segment breakpoints as round heads
mPaint.setAntiAlias(true); // Anti-aliasing
mPaint.setStyle(Paint.Style.STROKE); // Set hollow
mPaint.descent();// Yes baseline Distance from below to the lowest point of the character
mPaint.ascent();// Yes baseline The distance from above to the highest point of the character
2. Methods about canvas (Canvas)
How to create a new canvas
// Create directly 1 A canvas
Canvas canvas = new Canvas();
// Create 1 To specify bitmap Canvas with background
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Canvas canvas2 = new Canvas(bitmap);
Summary of some common methods of canvas 1
// Used to save Canvas The state of. save After that, you can call Canvas Translation, scaling, rotation, miscutting, cropping and other operations.
canvas.save();
// Used to recover Canvas The previously saved state. Prevent save Back pair Canvas The actions performed have an impact on subsequent drawing.
//save And restore To pair use ( restore Can be compared to save Less, but not more), if restore Call times ratio save More, it will cause Error .
canvas.restore();
// Move the origin of coordinates to the specified position
ccanvas.translate(x,y);
Summary of Common Drawing Methods
/** Draw arcs and sectors */
canvas.drawArc(oval,// Rectangle where arc is located
startAngle,// Starting angle
sweepAngle,// Angle of rotation
useCenter, //true It's painted in a fan shape, false It's drawn in an arc
paint);// Paintbrush
/** Draw text */
canvas.drawText(text, // Text content
x, y, // Draws text start coordinates (upper left corner)
paint);// A brush for drawing text
/** Draw a line */
canvas.drawLine(startX, startY,// Start xy Coordinates
stopX, stopY,// End point xy Coordinates
paint);// A brush for drawing lines
canvas.drawLines(pts,//// Draws an array of endpoints of lines, and each line occupies 4 Data.
paint);// Brushes for drawing
canvas.drawLines(pts,// Draws an array of endpoints of lines, and each line occupies 4 Data.
offset,// The number of data skipped, which will not participate in the drawing process.
count,// The number of data actually involved in drawing.
paint);// Brushes for drawing
/** Draw a rectangle */
canvas.drawRect(float x1,float y1,// Drawing vertex coordinates in the upper left corner of rectangle
float x2,float y2,// Drawing vertex coordinates in the lower right corner of rectangle
Paint paint) ;// Brushes for drawing
The above is the common method of Android drawing, hoping to help everyone's study.