Chapter 6 painting with White Paper-Canvas canvas (2)
6.2 common Canvas plotting Methods
In the previous section, we learned that if we create a canvas, we will draw it on it. The Canvas class of the Android SDK contains a series of methods for plotting. The methods are divided into three types. The following describes these commonly used painting methods.
1) Geometry is used to draw points, lines, rectangles, and arcs.
Some of the main methods are shown in Table 6-1:
Method |
Return Value |
Description |
DrawARGB (int a, int r, int g, int B) |
Void |
Fill the whole with a certain color |
DrawPoints (float [] pts, Paint paint) |
Void |
Draw a point |
DrawLines (float [] pts, Paint paint) |
Void |
Draw a line |
DrawRect (RectF rect, Paint paint) |
Void |
Draw a rectangle |
DrawCircle (float cx, float cy, float radius, Paint paint) |
Void |
Draw a circle |
DrawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint) |
Void |
Draw an arc |
DrawPath (Path path, Paint paint) |
Void |
Draw a shape by path |
Table 6-1 method for drawing geometric figures in the Canvas class
2) The Text method of the Canvas class is used to draw the Text content directly. The Text is usually represented by a string.
Some of the main methods are shown in Table 6-2. Several of the overload methods are text painting, but the parameters are different.
Method |
Return Value |
DrawText (String text, int start, int end, float x, float y, Paint paint) |
Void |
DrawText (char [] text, int index, int count, float x, float y, Paint paint) |
Void |
DrawText (String text, float x, float y, Paint paint) |
Void |
DrawText (CharSequence text, int start, int end, float x, float y, Paint paint) |
Void |
Table 6-2 How to draw text content in the Canvas class
3) The Bitmap method of the Canvas class is used to draw a Bitmap directly. Bitmap is usually represented by a Bitmap class.
Some of the main methods are shown in Table 6-3:
Method |
Return Value |
Description |
DrawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) |
Void |
Specify Matrix to draw bitmap |
DrawBitmap (int [] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint) |
Void |
Specify an array to be drawn as a Bitmap |
DrawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) |
Void |
Automatically scales to the drawing of the target rectangle |
Table 6-3 how to draw a bitmap in the Canvas class
Experience Sharing: Void drawLines (float [] pts, Paint paint) // draw a line Void drawRect (RectF rect, Paint paint) // draw a rectangle Note that the image drawn in the above method is left closed and right open. For example, to draw a RectF rect, the actually drawn rectangle is a rect. x, rect. y is the start coordinate, and the width and height are rect. right-1, rect. a rectangle of bottom-1. |
6.3 perform Canvas Transformation
Simple draw lines, rectangles, and circles all have ready-made methods to use, so we can do some complicated painting, such as rotation and scaling.
First, in the onDraw () method of View, we often see that the save () and restore () methods are called. What are they used?
Save (): used to save the Canvas status. After saving (), you can call operations such as pan, zoom, rotate, miscut, and crop of the Canvas.
Restore (): used to restore the State saved before the Canvas. Prevent operations performed on the Canvas after saving () from affecting subsequent painting.
---------------------------------------- It is difficult for programmers to make money. Therefore, they must learn financial management. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. We recommend investing in don't invest in security, which is almost impossible to transfer. It is very difficult to withdraw the website link in advance. Don't make it in white --------------------------------------------
Now that we know how to save and restore the Canvas, we can safely pan and scale the Canvas.
1) translate (float dx, float dy)
It is used to move the canvas and its origin to different locations. The default origin coordinate is (0, 0 ).
Parameters:
Dx, left and right offset (positive number is shifted to the right), in pixels
Dy, up/down offset (positive number is down), unit: pixel
2) rotate (float degrees)
It is used to rotate the canvas centered on the origin. The default origin coordinate is (0, 0 ).
3) rotate (float degrees, float px, float py)
Parameters:
Degrees, Rotation Angle
Px, set the abscissa of the rotation center (positive number is shifted to the right)
Py, set the vertical coordinates of the rotation center (positive number is downward movement)
4) scale (float sx, float sy );
Scale the canvas itself.
5) scale (float sx, float sy, float px, float py)
Parameters:
Sx, horizontal scaling
Sy, number-axis Scaling
Px, set the position of the origin (positive number is to move to the left, which is exactly the opposite of px in rotate)
Py, set the position of the origin (positive number is moved up, which is exactly the opposite of py in rotate)
6) Others
ClipPath (Pathpath), clipRect (Rect rect, Region. Op), clipRegion (Region region) and other methods similar to clipXXXX. It is used to set the valid area in the Canvas. It is draw on the invalid area and does not change the Canvas.
Experience Sharing: The save () and restore () methods are frequently used in the Canvas transformation operation. Note that save () and restore () must be paired (restore can be less than save, but not more). If restore () has more calls than save, an Error is thrown. Save () and restore () are usually mixed with special operations on Canvas. |