Android Definition (iii) Achieve the percentage setting of a disc

Source: Internet
Author: User
Tags getcolor

Recently have been learning their own definition of the control, yesterday saw a group of people asked how how to achieve 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, the corresponding three colors of the three angles

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 belong to their own definition of attributes, of course, you define the property to assign it a value

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 is, of course, the 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 ahead and define our own control. That's the end of the definition, how to 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 thing to note here is custom, which is your own definition of the property, before you declare xmlns:custom= "http://schemas.android.com/apk/res/your own package name"


Almost the same as this, the realization of the function you want, when you do not understand the code logic of others, you can debug, this is a very good way to


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




Android Definition (iii) Achieve the percentage setting of a disc

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.