Android self-defined view Learning (2)

Source: Internet
Author: User

The previous study of the basic use of methods, today to learn a little bit more complicated. Check it out first.

In order to complete the above effect is to use the previous period at the beginning of the four steps


1, the attribute should have color, must have the speed

<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr    name= "Speed" format= "integer"/>    <attr name= "Circlecolor" format= "color"/> <declare-styleable name=    "viewcircle" >        <attr name= "Speed"/>        <attr name= "Circlecolor"/>    </declare-styleable></resources>
Suppose there's nothing to know here to see my last Android Definition View learning (1)


2, get these properties in the constructor method

TypedArray a = Context.obtainstyledattributes (attrs,r.styleable.viewcircle, Defstyle, 0); int n = A.getindexcount (); for (int i = 0; i < n; i++) {int attr = A.getindex (i); switch (attr) {Case r.styleable.viewcircle_speed:mspeed = A.getinteger (attr); Break;case R . styleable. Viewcircle_circlecolor:mcolor = A.getcolor (attr, Color.green); A.recycle ();

3, this time we don't rewriteonmeasure


4 , rewrite OnDraw


protected void OnDraw (canvas canvas) {//Center Canvas.translate (getwidth ()/2, GetHeight ()/2);//Draw three round canvas.drawcircle (0, 0, Mcircle, canvas.drawcircle (0, 0, mcentrecircle), canvas.drawcircle (0, 0, mnexcircle)        ; Progress RECTF RECTF = new RECTF ( -180, -180, N/a); Canvas.drawarc (RECTF, -90, Mprogress, True, Mtimer);        Calculates the size of the font so that it is centered mlinepaint.gettextbounds (string.valueof (mprogress), 0,string.valueof (mprogress). Length (), rect); Canvas.drawtext (String.valueof (mprogress),-rect.width ()/2, 0,mlinepaint);//Total progress is 60 int count = 60;//scale from 190--200int y = 190; Rect rect = new rect (); Mlinepaint.gettextbounds ("xx", 0, "xx". Length (), rect);//When i%10==0, draw a tick mark for (int i = 0; i < Coun T i++) {if (i% = = 0) {canvas.drawline (0, y, 0, 0, mlinepaint), Canvas.drawtext (string.valueof (I/10) + "",-rect.width ()/2, mlinepaint);} Canvas.rotate (360/count, 0, 0);}}
Let's take a look at Canvas.drawarc (RECTF, -90, Mprogress, True, Mtimer);

The true effect we're passing in is



Suppose we pass in false.




Can obviously see the difference, nothing can also try our demo pass in false will be what effect.


And then we're going to use threads to refresh the UI to make this progress.

New Thread () {public void run () {while (true) {mprogress++;if (mprogress = =) {mprogress = 0;} Postinvalidate (); try {thread.sleep (mspeed);} catch (Interruptedexception e) {e.printstacktrace ();}}};}. Start ();

This is almost the same as the basic.

Next look at the effect



Project Source Code




Android self-defined view Learning (2)

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.