Android's custom control canvas parsing

Source: Internet
Author: User
Tags final

Custom controls are divided into two categories, custom ViewGroup controls, and custom view controls; The pace of tracking view can actually follow the Java implementation of the bottom of what we can find is only canvas, and then down is C + + or C implementation , so this article is mainly in the design of the angle to explain the relationship between canvas and view, and then a simple analysis of canvas usage;

View as the parent of all display views in Android, we can see its rendering method draw (Canvas Canvas), but also through the Canvas of drawing to achieve a variety of view display, so the various controls in Android such as: ImageView, Textview,edittextview and so on are through the canvas according to pass each parameter to draw the different view, thus forms each kind of different visual control; Of course, we can also draw some custom images with canvas by inheriting the OnDraw method of view. This is the simplest custom control and, of course, it requires a deep understanding of the event-handling logic of the view if various event responses are required.

Here's a simple metaphor: If you compare an Android interface to a window, then each view is a piece of glass, the whole frame is viewgroup, and the pattern on each piece of glass is what we call canvas, If there is no Canvas we can not see anything (only transparent glass), and receive all kinds of event processing or by the glass (View) receive processing, notice refresh pattern is (Canvas), this is a simple View and Canvas relationship. Clearly, their division of labor is clear, 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 show that need viewgroup participation. Here's how to learn the basic usage of canvas:

The basic usage of canvas is divided into two types:

1, directly in the view of the OnDraw method directly drawing, (this is a carrier view (the above example of the glass) directly to draw the pattern, such as we can make up the system control, as needed to add or modify the elements we need to

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

2, Canvas Canvas = new Canvas (), here is just a canvas for new, we can not see, it also can not handle any event, only to paint the modified image, as to display and receive event processing must have the carrier (View) only line;

Canvas has many of its own drawing methods, but ultimately it is calling the underlying code implementation: The following categories describe the common methods of canvas:

1, Save,savelayer,restore method to save and restore;

Save (int saveflags); You can specify which content to restore:

Source:

The Save_flag constants must match their native equivalents/** 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;

Savelayer (RECTF bounds, Paint Paint, int saveflags); Canvas Use this method for layer management, you can save the current layer based on the zone, after the drawing is completed, use Restoretocount to bind the layer;

2, Draw method;

Drawcolor,drawrgb,drawargb, drawing colors

Drawpath,drawlines,drawoval, such as drawing geometry;

Drawbitmap,drawpictur drawing pictures;

DrawText draw text;

3, Clipregion,clippath,cliprect cutting, here special attention to two kinds:

First, the meaning of various parameters in Region.op;

The second is the timing of clip (shearing), which is to finish cutting the canvas before the canvas is drawn, not the image;

4, there are some animation-related matrix operations, a total of divided into translate (translation), rotate (rotation), scale (scaling) and skew (tilt) four kinds; In this case, the relative coordinates are (0,0) when the matrix changes canvas, and we take note of the Canvas.translate (SCROLLX, scrolly) when we do any operation. Canvas.translate (- SCROLLX,-scrolly);

Canvas specific usage here no longer elaborated, the on-line material is many.

Related Article

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.