There are two types of custom controls that are custom ViewGroup controls, one that is custom view controls, and a way to track the view's pace to the bottom of the Java implementation. The only thing we can find is the canvas, and then the C + + or C implements the ; So this article is mainly to stand in the design of the perspective of the canvas and view the relationship between, and then simply analyze the canvas usage;
View as the parent of all the display views in Android, we can see how it is drawn (canvas canvas), which is nothing more than canvas drawing to achieve a variety of view display, so the various controls in Android such as: ImageView, Textview,edittextview, and so on, is to draw different views based on the various parameters passed through the canvas, thus forming a variety of visual controls; Of course we can also use canvas to draw some custom images by inheriting the OnDraw method of view. This is the simplest custom control, of course, if you need a variety of event responses, you need a deep understanding of the event handling logic of the view.
Here's a simple analogy: if an Android interface is likened to a window, then every view is a piece of glass, and the whole frame is viewgroup, and every piece of glass is the canvas we're talking about, If there is no Canvas we can not see anything (only transparent glass), and receive a variety of event processing or glass (View) received processing, notification refresh pattern is (canvas), this is the simple View and canvas relationship. Clearly, they have a clear division of work, and the view is only responsible for handling event interactions, and canvas is only responsible for displaying and refreshing images based on the parameters provided by the view. Of course if you want multiple view at the same time, then you need viewgroup to participate. Here's a look at the basic uses of canvas:
The basic usage of canvas is divided into two types:
1, directly in the view of the OnDraw method directly in the drawing, (this is a carrier view (the above example of the glass) to draw a pattern directly, for example, we can make a copy of the system control, as needed to add or modify the elements we need.
2, canvas canvas = new canvas (), here is just a new canvas, we do not see, it can not handle any event, just paint the modified image, as for the display and receive event processing must have a carrier (View);
Canvas has many of its own drawing methods, but it is ultimately called the underlying code implementation: The following categories describe the common methods of canvas:
1, Save,savelayer,restore method preservation and restoration;
Save (int saveflags); You can specify which content to restore:
The Save_flag constants must match their native equivalents /** restore the current matrix if restore () is called */public static final int matrix_save_flag = 0x01; /** Restore the current clip, restore () is called */public static final int clip_save_flag = 0x02; /** the layer needs to per-pixel Alpha */public static final int has_alpha_layer_save_flag = 0x04; /** the layer needs to 8-bits per color component */public static final int full_color_layer_save_flag = 0x08; /** clip against the layer ' s bounds */public static final int clip_to_layer_save_flag = 0x10; /** Restore everything when restore () is called */
Savelayer (RECTF bounds, paint paint, int saveflags); Canvas Use this method for layer management, you can save the current layer based on the region, after drawing is completed, the layer is bound by restoretocount;
2, Draw method;
Drawcolor,drawrgb,drawargb, drawing colors
Drawpath,drawlines,drawoval and other drawing geometry;
Drawbitmap,drawpictur drawing pictures;
DrawText drawing text;
3, Clipregion,clippath,cliprect cutting, here are special attention to two kinds:
One is the meaning of various parameters in Region.op;
The second is the time of clip (cut), is to finish the canvas before the canvas is cropped, not the image;
4, there are some of the matrix operation related to animation, divided into translate (translation), rotate (rotation), scale (scaling) and skew (tilt) four kinds; Here is a special explanation, here Canvas for matrix changes when the relative coordinates (0,0); When we do anything, note canvas.translate (SCROLLX, scrolly); Canvas.translate (- SCROLLX,-scrolly);
Canvas concrete usage Here no longer elaborated, the online material is many.