Android Custom View imitation QQ level days progress

Source: Internet
Author: User
Tags drawtext

I've been looking at the custom view piece lately. It's been almost one weeks. This week, keep updating the blog every day, feel that their technology has a bit of breakthrough, the custom view of the calculation also has a deeper understanding.

Today, when you take the subway to play mobile phone, see the mobile phone a growing number of days progress of the control, feel very interesting, so I want to write one. The effect is as follows:

As you can see from the diagram, there are many elements in it, first the rectangular area of the background, followed by the two rings, and then the three text text. Actually not complicated, we 1.1 point to achieve.

First, draw a rectangular background. Here you use a RECTF class that contains four single-precision floating-point coordinates for a rectangle. The rectangle represents a rectangle by the coordinates of the top and bottom 4 sides. These coordinate value properties can be accessed directly, using the width () and height () methods to get the width and height of the rectangle, and he also has the construction method:

RECTF altogether has four construction methods:

RECTF () constructs an argument-free rectangle

RECTF (float left,float top,float right,float bottom) Constructs a rectangle that specifies 4 arguments

RECTF (Rect F R) constructs a RECTF object based on the specified RECTF object (the left coordinate of the object is unchanged)

RECTF (Rect R) constructs a RECTF object based on the given Rect object

So, here's the second construct, the code is as follows:

    
      (W,  H,  oldw,  oldh) {
        . onsizechanged (W, H, OLDW, OLDH);
        Mwidth=getwidth ();
        Mheight=getheight ();
        Mrectf= RECTF (() (mwidth*), () (mheight*), () (mwidth*), () (mheight*));

    

Now the background of the rectangle is there, so, there are rings and words and how to deal with it, don't worry, we first look at this ring, I am here to define two rings, because, one is used as a progress to show, one as a background to achieve, well, draw two rings respectively. The code is as follows:

    Canvas.drawarc (MRECTF,,,, mbuttompaint);

    Canvas.drawarc (MRECTF,,,, Mtoppaint);

Two rings also have, Next is the text, draw the text we use canvas. DrawText method, to draw, the specific code is as follows:

. DrawText (mwidth-(Mtextwidth+mtextsmail))/, (mheight/), mtextpaint);

. DrawText (mwidth-(mtextwidth+mtextsmail))/+mtextwidth, () (mheight/), msmailtextpaint);

DrawText (, (Mwidth-mtextsmailbuttom)/, () (mheight/+), msmailtextpaint);

The whole effect comes out, and finally, I put all the code on it:

     {Paint mbuttompaint;

     Url:http://www.bianceng.cn/os/extra/201608/50371.htm Paint Mtoppaint;

     Paint Mtextpaint;

      Paint Msmailtextpaint;

      Mwidth;

     Mheight;


     RECTF MRECTF; (Context, AttributeSet attrs, defstyleattr)

        {(context, attrs, defstyleattr);

    Initview ();
        (Context, AttributeSet attrs) {(context, attrs);
    Initview ();
        } (context context);
    Initview ();
        } () {mbuttompaint= Paint ();
        Mbuttompaint.setcolor (Color.rgb (,,));
        Mbuttompaint.setantialias ();
        Mbuttompaint.setstrokewidth ();

        Mbuttompaint.setstyle (Style.stroke);
        Mtoppaint= Paint ();
        Mtoppaint.setcolor (Color.parsecolor ());
        Mtoppaint.setantialias ();
        Mtoppaint.setstrokewidth ();

        Mtoppaint.setstyle (Style.stroke);
        Mtextpaint= Paint ();
        Mtextpaint.setcolor (Color.White); MTextpaint.setantialias ();
        Mtextpaint.setstrokewidth ();

        Mtextpaint.settextsize ();
        Msmailtextpaint= Paint ();
        Msmailtextpaint.setstrokewidth ();
        Msmailtextpaint.setcolor (Color.White);
        Msmailtextpaint.setantialias ();

    Msmailtextpaint.settextsize ();
        (W, H, OLDW, OLDH) {. onsizechanged (W, H, OLDW, OLDH);
        Mwidth=getwidth ();
        Mheight=getheight ();

    Mrectf= RECTF (() (mwidth*), () (mheight*), () (mwidth*), () (mheight*));

        } (Canvas Canvas) {. OnDraw (Canvas);

        Canvas.drawarc (MRECTF,,,, Mbuttompaint);

         Canvas.drawarc (MRECTF,,,, Mtoppaint);

         Mtextwidth=mtextpaint.measuretext ();

         Mtextsmail=msmailtextpaint.measuretext ();

        Mtextsmailbuttom=msmailtextpaint.measuretext ();

        Canvas.drawtext (, (mwidth-(Mtextwidth+mtextsmail))/, () (mheight/), mtextpaint); Canvas.drawtext (mwidth-(mtextwidth+mtextsmail))/+mtextwidth, () (mheight/), msmailtextpaint);

    Canvas.drawtext (, (Mwidth-mtextsmailbuttom)/, () (mheight/+), msmailtextpaint); }


}

Thanks for reading. More technology, you can pay attention to my other articles, thank you for your support.

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.