Android Custom (iii) achieve the percentage setting of a disc

Source: Internet
Author: User

Recently have been learning custom control, yesterday saw a group of people asked how to implement the disk style display, learn to use, and then have this blog

First, at a glance


You can set the display color and the size of the color block.

There are three colors, which correspond to the three angles of three different colors.

On the code:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable    name= "Customcircle" >        <attr name= "firstcolor" format= "color"/> <attr name=        "secondcolor" format= "color"/>        < attr name= "Thirdcolor" format= "color"/> <attr name=        "firstangle" format= "integer"/>        <attr name= " Secondangle "format=" integer "/>        <attr name=" thirdangle "format=" integer "/>    </ Declare-styleable></resources>
All of the above are custom attributes, and of course the custom attribute will be assigned to it.

TypedArray Marray = context.obtainstyledattributes (attrs,r.styleable.customcircle, defstyleattr, 0); FirstColor = Marray.getcolor (r.styleable.customcircle_firstcolor,color.blue); secondcolor = Marray.getcolor ( R.styleable.customcircle_secondcolor,color.green); Thirdcolor = Marray.getcolor (r.styleable.customcircle_ thirdcolor,color.red); Firstangle=marray.getint (r.styleable.customcircle_firstangle); secondAngle= Marray.getint (R.styleable.customcircle_secondangle), Thirdangle=marray.getint (R.styleable.customcircle_ Thirdangle, Marray.recycle ();
After the property assignment is finished, the most important part of course is to start, drawing, i.e. overriding the OnDraw () method

@Overrideprotected void OnDraw (canvas canvas) {int center=getwidth ()/2;int Radius=center/2;mpaint.setcolor ( Color.gray); Mpaint.setstrokewidth (center); Mpaint.setantialias (true); Mpaint.setstyle (Paint.Style.STROKE); Canvas.drawcircle (center, center, Radius, mpaint); Mpaint.setcolor (Firstcolor); RECTF rectf=new RECTF (Center-radius, Center-radius, Center+radius, Center+radius); Canvas.drawarc (RectF, 0, FirstAngle , false, Mpaint); Mpaint.setcolor (Secondcolor); Canvas.drawarc (RECTF, Firstangle, Secondangle, False, Mpaint); Mpaint.setcolor (Thirdcolor); Canvas.drawarc (RECTF, Secondangle, Thirdangle, False, Mpaint);}

Let's go on, the custom control just ends with that definition, how do you use it? People who have read the previous blog should know!

<com.sdufe.thea.guo.view.customcircle        android:layout_width= "300DP"        android:layout_height= "300DP"        custom:firstcolor= "@android: Color/holo_purple"        custom:secondcolor= "@android: Color/holo_blue_bright"        custom:thirdcolor= "@android: Color/holo_orange_light"        custom:firstangle= "        custom:secondangle=" "        custom:thirdangle="/>

The note here is custom, this is your custom property, before you declare xmlns:custom= "http://schemas.android.com/apk/res/your own package name"


It's pretty much the way you do it, you can debug it when you can't read someone else's code logic, and that's a good way to do it.


Code: http://download.csdn.net/detail/elinavampire/8175771




Android Custom (iii) achieve the percentage setting of a disc

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.