Common methods of Canvas and Paint

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html

1. First, let's talk about the canvas class:

Class Overview

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing). 

This class is equivalent to a canvas, you can draw many things in it;

We can understand this Canvas as a memory area provided by the system (but in fact it is just a set of drawing APIs, the real memory is the Bitmap below), and it also provides a complete set of operations on this memory area. methods, and all of these operations are drawing APIs. That is to say, in this way, we can already draw a stroke or use Graphic to draw what we need, and we can control what we want to draw and what we want to display.

 

This method is also divided into two types according to the environment: one is to use the canvas of the ordinary View to draw, and the other is to use the canvas of the special SurfaceView to draw. The main difference between the two is that a dedicated thread can be defined in the SurfaceView to complete the drawing work, and the application does not need to wait for the View to brush the map, which improves performance. The former is suitable for animations with relatively small processing volume and low frame rate, such as chess games; the latter is mainly used for games and high-quality animations.

The following are commonly used methods of the Canvas class:

 

drawRect(RectF rect, Paint paint) //Drawing area, parameter 1 is an area of ​​RectF 

drawPath(Path path, Paint paint) //Draw a path, parameter one is the Path path object

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) //Texture, the first parameter is our regular Bitmap object, the second parameter is the source area (here is the bitmap), and the third parameter is the target area (should be in the canvas position and size), the fourth parameter is the Paint brush object, because the possibility of scaling and stretching is used, when the original Rect is not equal to the target Rect, there will be a significant loss in performance.

drawLine(float startX, float startY, float stopX, float stopY, Paintpaint) //Draw a line, the x-axis position of the starting point of parameter 1, the y-axis position of the starting point of parameter 2, the horizontal position of the x-axis of the end point of parameter 3, parameter 1 The vertical position of the four y-axis, and the last parameter is the Paint brush object.

drawPoint(float x, float y, Paint paint) //Draw a point, parameter 1 is the horizontal x-axis, parameter 2 is the vertical y-axis, and the third parameter is the Paint object.

drawText(String text, float x, floaty, Paint paint) //Rendering text, Canvas class can also draw text in addition to the above, parameter 1 is the text of type String, parameter 2 x axis, parameter 3 y axis, parameter 1 Number four is a Paint object.

drawOval ( RectF  oval,  Paint  paint)//Draw an ellipse, the first parameter is the scan area, and the second parameter is the paint object;

drawCircle (float cx, float cy, float radius, Paint  paint)// Draw a circle, the first parameter is the x-axis of the center point, the second parameter is the y-axis of the center point, the third parameter is the radius, and the fourth parameter is paint object;

drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)//画弧,

The first parameter is the RectF object, the boundaries of a rectangular area ellipse are used to define the shape, size, and arc, and the second parameter is the starting angle (degrees) at the beginning of the arc,

Parameter 3 the sweep angle (degrees) to start measuring clockwise, Parameter 4 is if this is true, include the arc in the center of the ellipse, and close it, if it is false it will be an arc, Parameter 5 is Paint object;

Also understand a paint class:

 

Class Overview

The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 

The paint class holds style and color information on how to draw geometry, text and bitmaps.

 

 

Paint represents brushes, brushes, paints, etc. on Canvas;

 

Common methods of the Paint class:

setARGB(int a, int r, int g, int b) // Set the color of the Paint object, parameter 1 is the alpha transparent value

setAlpha(int a) // Set the alpha opacity, the range is 0~255

setAntiAlias(boolean aa) // Whether to anti-alias

setColor(int color) // Set the color, here the Color class defined inside Android contains some common color definitions

setTextScaleX(float scaleX)  // 设置文本缩放倍数,1.0f为原始

setTextSize(float textSize)  // 设置字体大小

setUnderlineText(booleanunderlineText)  // 设置下划线

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326604732&siteId=291194637