Android custom widget _ Canvas Analysis

Source: Internet
Author: User

Custom Controls are divided into two types: Custom ViewGroup controls and custom View Controls; tracking the pace of View can actually follow the Java Implementation below we can find that only Canvas, and then it is C ++ or C implementation; therefore, this article mainly explains the relationship between Canvas and View from the design perspective, and then briefly analyzes the Canvas usage. View is the parent class of all display views in Android, we can see that in its drawing method draw (Canvas canvas), it is nothing more than drawing a Canvas to display various views, so various Android controls such as: ImageView, TextView, editTextView and so on are used to draw different views based on each parameter passed through the Canvas to form different visual controls. Of course, we can also inherit the onDraw method of the View, using Canvas to draw custom images is the simplest custom control. Of course, if you need various event responses, you need to have a deep understanding of the View. Processing logic. Here is a simple analogy: if an Android interface is compared to a window, each View is a piece of glass, and the entire framework is ViewGroup, the pattern on each piece of glass is what we call Canvas. If there is no Canvas, we can't see anything (only transparent glass), and the processing of various events is still handled by glass (View) after receiving and processing, the notification refresh pattern is Canvas, which is the relationship between a simple View and Canvas. Obviously, they have a clear division of labor. View only processes event interaction, while Canvas only displays and refreshes images based on the parameters provided by View. Of course, if you want to display multiple views at the same time, VIewGroup is required. The basic usage of Canvas is as follows: 1. Draw the Canvas directly in the onDraw method of View. (This is a carrier View (the glass in the above example) for example, you can re-write the system control and add or modify the required elements as needed.) 2. Canvas canvas = new Canvas (), this is just a canvas of New. We cannot see it, nor can it process any event. We only need to plot the modified image. As for display and receive event processing, we must have a vector (View; canvas has many of its own painting methods, but it eventually calls the underlying code implementation: The following describes common Canvas methods: 1. save, saveLayer, and restore methods for saving and restoring; save (int saveFlags); specifies which content to restore: source code: copy the code // the SAVE_FLAG constants must match their native equiv Alents/** restore the current matrix when restore () is called */public static final int MATRIX_SAVE_FLAG = 0x01;/** restore the current clip when 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 */public static final int ALL_SAVE_FLAG = 0x1F; copy the code saveLayer (RectF bounds, Paint paint, int saveFlags); Canvas uses this method to manage layers, you can save the current layer according to the region, and use restoreToCount to bind the layer after the painting is complete; 2. draw method; drawColor, drawRGB, drawARGB, drawing color drawPath, drawLines, dr Draw geometric figures such as awOval, drawBitmap, drawPictur, and drawText. 3. clipRegion, clipPath, and clipRect cropping. There are two types of special attention: Region. the meanings of various parameters in Op; the second is the Clip (CUT) time, which is to crop the canvas before the canvas is drawn, rather than the image; 4. There are also animation-related Matrix operations, which can be divided into four types: translate, rotate, scale, and skew; here, we will specifically explain that the relative coordinate of the Canvas when matrix is changed is (0, 0); when we perform any operation, pay attention to the canvas. translate (scrollX, scrollY); canvas. the meaning of translate (-scrollX,-scrollY. The usage of Canvas is not described here. There are a lot of online materials ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.