Canvas for Android programming draws various graphics (point, line, arc, circle, ellipse, text, rectangle, polygon, curve, rounded rectangle)

This paper describes the method of drawing various graphics in canvas of Android programming. Share with you for your reference, as follows:

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, in which you can draw a lot of things;

We can understand this canvas as a memory area provided by the system (but in fact, it is only a set of drawing API, and the real memory is the following bitmap), and it also provides a complete set of methods to operate this memory area. All these operations are drawing API. In other words, in this way, we can draw what we need one stroke or use graphic. What we want to draw and display is controlled by ourselves.

This method can also be divided into two types according to the environment: one is to use the canvas of ordinary view to draw pictures, and the other is to use the canvas of special surfaceview to draw pictures. The main difference between the two is that a special thread can be defined in the surfaceview to complete the drawing work. The application does not need to wait for the view to brush the drawing, so as to improve the performance. The previous one is suitable for animation with small processing capacity and small frame rate, such as chess games; The latter is mainly used in games and high-quality animation.

The following are the common methods of canvas class:

DrawRect (rectf, rect, paint paint) / / paint an area. Parameter 1 is a rectf area. Drawpath (path path, paint paint) / / paint a path. Parameter 1 is a path object. Drawbitmap (bitmap, rect SRC, rect DST, paint paint paint) / / map. Parameter 1 is our regular bitmap object. Parameter 2 is the source area (bitmap here). Parameter 3 is the target area (it should be at the position and size of the canvas). The fourth parameter is the paint brush object. Because scaling and stretching may be used, the performance will be greatly lost when the original rect is not equal to the target rect. DrawLine (float startx, float starty, float stopx, float stopy, paintpaint) //Draw a line. The parameters are the x-axis position of the starting point, the y-axis position of the starting point of parameter 2, the x-axis horizontal position of the end point of parameter 3, and the y-axis vertical position of parameter 4. The last parameter is the paint brush object. Drawpoint (float x, float y, paint 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, float y, paint paint) / / render text. Canvas class can also draw text in addition to the above. Parameter 1 is string type text. Parameter 2 is x-axis, parameter 3 is y-axis, and parameter 4 is paint object. DrawOval (rectf oval, paint paint) / / draw ellipse. Parameter 1 is scanning area, parameter 2 is paint object; drawcircle (float CX, float cy, float radius, paint paint) / / draw a circle. Parameter 1 is the x-axis of the center point, parameter 2 is the Y-axis of the center point, parameter 3 is the radius, and parameter 4 is the paint object; drawarc (rectf oval, float startangle, float sweep angle, Boolean usecenter, paint paint) //Draw an arc. The first parameter is rectf object, a rectangular area, and the oval boundary is used to define the shape, size and arc. The second parameter is the starting angle (degree) at the beginning of the arc, and the third parameter is the scanning angle (degree) Start to measure clockwise. Parameter 4 is if this is true, including the arc in the center of the ellipse, and turn it off. If it is false, it will be an arc, and parameter 5 is the 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 has style and color information about how to draw geometry, text, and bitmaps. Paint represents the brushes, brushes, pigments, etc. on canvas;

Common methods of paint class:

Setargb (int a, int r, int g, int b) / / set the color of the paint object. Parameter 1 is the alpha transparency value setalpha (int a) / / set the alpha opacity, ranging from 0 to 255 setantialias (Boolean AA) / / anti aliasing SetColor (int color) / / set the color. The color class defined inside Android contains some common color definitions settextscalex (float scaleX) //Set the text scaling factor, 1.0F as the original settextsize (float textsize) / / set the font size setunderlinetext (Boolean underlinetext) / / set the underline

2. Look at the case directly

Take a look at the renderings:

In this case, we use the custom view class:

CustomActivity.java

The important class custom view component needs to override the OnDraw (canvas) method of the view component. Next, draw a large number of geometric shapes on the canvas, such as points, lines, arcs, circles, ellipses, text, rectangles, polygons, curves, rounded rectangles, etc!

DrawView.java

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>