Take your hand and draw you a dynamic Error alert Android Custom view

Source: Internet
Author: User

Well.. 1 more to get the perseverance badge, today to draw you a relatively simple view.


Reprint Please specify source: http://blog.csdn.net/wingichoy/article/details/50477108

Don't say much nonsense, look:


First, the constructor measures ... Here's a pen.

Public Errorview (Context context) {This (context, NULL);    } public Errorview (context context, AttributeSet Attrs) {This (context, attrs, 0);     } public Errorview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int widthsize = Measu        Respec.getsize (WIDTHMEASURESPEC);        int widthmode = Measurespec.getmode (Widthmeasurespec);        int heightsize = measurespec.getsize (Heightmeasurespec);        int heightmode = Measurespec.getmode (Heightmeasurespec);        if (Widthmode = = measurespec.exactly) {mwidth = widthsize;        } else {mwidth = 200;        } if (Heightmode = = measurespec.exactly) {mheight = heightsize;        } else {mheight = 200;    } setmeasureddimension (Mwidth, mheight); }
If you do not understand the above, turn over my blog, there are some complementary knowledge points. Round percent view


Next draw, if you want to draw a static this suddenly, you must close the eyes can also draw out. So how can we achieve dynamic effects?


In fact, imitation of the process of hand-painted, we are 1.1 points drawn out, a line of gradual delay. Then we will imitate this natural process.

First draw a circle.

Paint p = new paint ();        P.setstrokewidth (ten);        P.setantialias (true);        P.setcolor (color.red);        P.setstyle (Paint.Style.STROKE);        RECTF RECTF = new RECTF (0 + ten, 0 +, mWidth-10, mHeight-10);        Canvas.drawarc (RECTF, mprogress/100, p);        mprogress+=5;

You can see that the third parameter of DrawArc is a change, where the initial value of Mprogress is zero, and here he increments, that is, every time the OnDraw method is called, he increases.  So each time the arc will be a little longer than the original, until the end of the painting. So at the end of the program there must be postinvalidatedelayed (10); Method.


Next to draw two lines, where the coordinates I directly take the radius of one of the 4 points, the only thing to note is that only when the progress is greater than 100 when we draw two lines, two line segments are also based on a variable self-increment, the principle of the same. Here Mlineonex and other parameters all indicate the coordinates of two points when drawing the line. When Mlineonex = mwidth * 0.5 Mwidth/4 + Mlineonex is equal to the final point of the line we want to draw.



if (Mprogress >) {            //Draw left line            if (Mlineonex < Mwidth * 0.5) {                mlineonex+=20;                mlineoney+=20;            }            Canvas.drawline (MWIDTH/4, MHEIGHT/4, MWIDTH/4 + Mlineonex, MHEIGHT/4 + Mlineoney, p);            if (Mlineonex = = Mwidth * 0.5) {                if (Mlinetwox < Mwidth * 0.5) {                    mlinetwox+=20;                    mlinetwoy+=20;                } else {                    //Judging all draw complete                    Islinedrawdone = true;                }                Canvas.drawline (MWIDTH/4, (float) (Mheight * 0.75), MWIDTH/4 + Mlinetwox, (float) (Mheight * 0.75)-Mlinetwoy, p); 
   }        }
Then add a marker bit islinedrawdone to judge if you have not finished the painting:

if (islinedrawdone) {            log.e ("Wing", "Draw Done");                   } else{            postinvalidatedelayed (ten);        }

Now basically completed the drawing, do not hurry to add vibration, vibration effect is how to achieve it, you remember, if you forget, you can look at my blog: Custom Animation to achieve a shake shake effect

So we're going to write an interface that onstop back and forth.

public interface onstoplistener{        void OnStop (View v);    }

Complete the final drawing and continue to add a flag that represents the complete drawing.

if (islinedrawdone) {            log.e ("Wing", "Draw Done");            if (!isdrawdone) {                if (monstoplistener! = null) {                    monstoplistener.onstop (this);                }                Isdrawdone = true;            }        } else{            postinvalidatedelayed (ten);        }

Provides a reset () method that allows the user to manually control the redraw

public void Reset () {        mprogress = 0;        Mlineonex = 0;        Mlineoney = 0;        Mlinetwox = 0;        Mlinetwoy = 0;        Islinedrawdone = false;        Isdrawdone = false;        Invalidate ();    }

To provide a way to add a listener

public void Setonstoplistener (Onstoplistener onstoplistener) {        monstoplistener = Onstoplistener;    }



Finally, add a vibration effect to the view in activity

protected void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Merrorview = (Errorview) Findviewbyid (R.id.errorview);        Merrorview.setonstoplistener (New Errorview.onstoplistener () {            @Override public            void OnStop (View v) {                Shakeanimation sa = new shakeanimation ();                Sa.setduration (+);                V.startanimation (SA);            }        );        Merrorview.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (View v) {                Merrorview.reset ();            }        });

Hehe hey ... This has been a cheat on the medal of Perseverance.

Source Address: http://download.csdn.net/detail/wingichoy/9394685

Take your hand and draw you a dynamic Error alert Android Custom view

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.