Deep understanding of Custom view from Android drawing View small Example

Source: Internet
Author: User

When developing the interface, the Android system provides us with a variety of view components, TextView, ImageView, Button, LinearLayout, ScrollView, ListView, etc. These also basically meet the usual development requirements. Sometimes the development will also encounter more tricky requirements, custom view will be used more or less.

It's not easy to get a thorough understanding of a custom view, because it involves sizing, location, Canvas, matrix calculations, and so on, with a lot of content. I remember that when I first started Android, I quickly took a custom view and found that it wasn't enough.

1, first configure the view size and location

1) For example, to achieve a square view, keep the height and width consistent

    @Override    protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {        int widthsize = Measurespec.getsize (WIDTHMEASURESPEC);        Heightmeasurespec = Measurespec.makemeasurespec (widthsize, measurespec.exactly);        Super.onmeasure (Widthmeasurespec, Heightmeasurespec);    }

2) Configure the view location in the parent view, using the OnLayout

    @Override    protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {        super.onlayout (chan Ged, left, top, right, bottom);    }

2, before you draw the content, you have to prepare the brush

        Prpaint = new Paint ();        Anti-aliasing        Prpaint.setantialias (true);        Anti-jitter        Prpaint.setdither (true);        Prpaint.setstrokecap (Paint.Cap.ROUND);        Prpaint.setstyle (Paint.Style.STROKE);        Prpaint.setstrokewidth (pr_stroke_width);        dot = new Path ();        Specify clockwise direction (CW)        dot.addcircle (0, 0, DOTTED_LINE_WIDTH/2, Path.Direction.CW);        Dotpaint = new Paint ();        Dotpaint.setantialias (true);        Dotpaint.setdither (true);        Dotpaint.setstyle (Paint.Style.STROKE);        Dotpaint.setcolor (color.white);        Innercircle = new Paint ();        Innercircle.setantialias (true);        Innercircle.setdither (true);        Innercircle.setstyle (Paint.Style.STROKE);        Innercircle.setstrokewidth (inner_line);        Innercircle.setcolor (themecolor);        Effects_line = new Dashpatheffect (new float[]{30, 10}, 1);

3. Draw the content through the canvas

    @Override public void Draw (canvas canvas) {super.draw (canvas);        Dotdrawrect.set (Dotrect);        Final float CenterX = Dotdrawrect.centerx ();        Final float centery = Dotdrawrect.centery ();        float radius = dotdrawrect.width ()/2;        Initpatheffect ();        Dotpaint.setpatheffect (EFFECTS1);        Canvas.drawcircle (CenterX, CenterY, radius, dotpaint);        radius = radius-dotted_line_padding;        Dotpaint.setpatheffect (EFFECTS2);        Canvas.drawcircle (CenterX, CenterY, radius, dotpaint);        radius = radius-dotted_line_padding;        Dotpaint.setpatheffect (EFFECTS3);        Canvas.drawcircle (CenterX, CenterY, radius, dotpaint);        radius = radius-dotted_line_padding;        Dotpaint.setpatheffect (EFFECTS4);        Canvas.drawcircle (CenterX, CenterY, radius, dotpaint);        radius = radius-dotted_line_padding;        Innercircle.setpatheffect (NULL);        Innercircle.setcolor (Color.White); Canvas.drawcircle (CenterX, CEntery, Radius, innercircle);        Radius = (float) (radius-dotted_line_padding * 1.5);        Innercircle.setpatheffect (Effects_line);        Innercircle.setcolor (ThemeColor);        Canvas.drawcircle (CenterX, CenterY, radius, innercircle);        Radius = RADIUS-PR_STROKE_WIDTH/2-DOTTED_LINE_PADDING/2;        if (Powinterradius = = 0) {Powinterradius = Math.pow (radius, 2);        }//progress bar Draw Dotdrawrect.set (Centerx-radius, Centery-radius, CenterX + radius, centery + radius);        Prpaint.setcolor (Color.gray);        Canvas.drawarc (Dotdrawrect, N, +, False, prpaint);        Prpaint.setcolor (Color.White);    Canvas.drawarc (Dotdrawrect, Progress * 3, FALSE, Prpaint); }

4, if you need to interact with the user, you also need to handle touch, click on these events (then carefully analyzed later)

Source: Http://git.oschina.net/hanbingsheshou/SimpleDraw



Deep understanding of Custom view from Android drawing View small Example

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.