Android own definition view (iv) Video volume control

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/24529807

Today is nothing to stroll EoE, see someone ask for help to do a following effect, I see the following a buddy said to use 12 pictures, this Nima trouble, meticulous look at the feeling of their own definition control wood has a problem, take a moment to write a.


Well, get to the point and continue our own definition of view four.

1, the First division of the required attributes, two small pieces of color, a picture in the middle, the gap size, a number of blocks. Analysis complete, start writing Attr.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <attr name= "firstcolor" format= "Color"/>    <attr name= "secondcolor" format= "color"/> <attr name=    "Circlewidth" format= "Dimension"/>    <attr name= "Dotcount" format= "integer"/>    <attr name= "splitsize" format= "integer"/>    <attr Name= "BG" format= "reference" ></attr>    <declare-styleable name= "Customvolumcontrolbar" >        <attr name= "Firstcolor"/>        <attr name= "Secondcolor"/>        <attr name= "Circlewidth"/>        <attr name= "Dotcount"/>        <attr name= "splitsize"/> <attr        name= "bg"/>    </ Declare-styleable></resources>

2. Get these attributes in the construct:

/** * The color of the first circle */private int mfirstcolor;/** * The color of the second circle */private int msecondcolor;/** * Circle width */private int mcirclewidth;/** * Brush */private Paint mpaint;/** * Current progress */private int mcurrentcount = 3;/** * Middle picture */private Bitmap mimage;/** * gap between each block block */private int msplitsize;/** * number */private int mcount;private Rect mrect;public Customvolumcontrolbar (context context, at Tributeset attrs) {This (context, attrs, 0);} Public Customvolumcontrolbar (Context context) {This (context, null);} /** * Necessary initialization, get some of your own defined values * * @param context * @param attrs * @param defstyle */public customvolumcontrolbar (Context Contex T, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); TypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.customvolumcontrolbar, DefStyle, 0); int n = A.getindexcount (); for (int i = 0, i < n; i++) {int attr = A.getindex (i); switch (attr) {case R.styleable.customvolumcont Rolbar_firstcolor:mfirstcolor = A.getcolor (attr, color.green); Break;case R.styleable.customvoLumcontrolbar_secondcolor:msecondcolor = A.getcolor (attr, Color.cyan); Break;case R.styleable.customvolumcontrolbar _bg:mimage = Bitmapfactory.decoderesource (Getresources (), A.getresourceid (attr, 0)); break;case R.styleable.customvolumcontrolbar_circlewidth:mcirclewidth = A.getdimensionpixelsize (attr, (int) Typedvalue.applydimension (TYPEDVALUE.COMPLEX_UNIT_PX, Getresources () getdisplaymetrics ()); break;case R.styleable.customvolumcontrolbar_dotcount:mcount = A.getint (attr, 20);//Default 20break;case R.styleable.customvolumcontrolbar_splitsize:msplitsize = A.getint (attr); break;}} A.recycle (); mpaint = new Paint (); mrect = new Rect ();}

3. Rewrite OnDraw

@Overrideprotected void OnDraw (canvas canvas) {Mpaint.setantialias (true);//anti-aliasing mpaint.setstrokewidth (mcirclewidth) ; Sets the width of the ring mpaint.setstrokecap (Paint.Cap.ROUND); Defines the segment's power-off shape as a round head mpaint.setantialias (TRUE); Anti-aliasing Mpaint.setstyle (Paint.Style.STROKE); Set Hollow int centre = getwidth ()/2; Gets the x coordinate of the center of int radius = centre-mcirclewidth/2;//Radius/** * Draw block block to */drawoval (canvas, centre, RADIUS);/** * Calculates the position of the inner tangent square */in T Relradius = radius-mcirclewidth/2;//Gets the radius of the inner circle/** * The distance from the inner tangent square top = mcirclewidth + Relradius-√2/2 */mrect.left = (int ) (RELRADIUS-MATH.SQRT (2) * 1.0F/2 * Relradius) + mcirclewidth;/** * Inner tangent square distance left = mcirclewidth + Relradius-√2/2 */ Mrect.top = (int) (RELRADIUS-MATH.SQRT (2) * 1.0F/2 * Relradius) + Mcirclewidth;mrect.bottom = (int) (Mrect.left + Math . sqrt (2) * Relradius); mrect.right = (int) (Mrect.left + math.sqrt (2) * Relradius);/** * If the picture is smaller, then place it in the center of the image according to the size of the picture */if (Mima Ge.getwidth () < MATH.SQRT (2) * Relradius) {mrect.left = (int) (Mrect.left + math.sqrt (2) * Relradius * 1.0F/2-Mimage.getwidth () * 1.0F/2); mrect.top = (int) (Mrect.top + math.sqrt (2) * Relradius * 1.0F/2-Mimage.geth Eight () * 1.0F/2); mrect.right = (int) (Mrect.left + mimage.getwidth ()); mrect.bottom = (int) (Mrect.top + Mimage.getheigh T ());} Draw Canvas.drawbitmap (mimage, NULL, Mrect, mpaint);} /** * Draw each small block according to the number of participants * * @param canvas * @param centre * @param radius */private void drawoval (canvas canvas, int centre, in T radius) {/** * calculates the percentage of each block based on the number of draws required and the gap *360 */float itemsize = (* 1.0f-mcount * msplitsize)/mCount; RECTF Oval = new RECTF (Centre-radius, Centre-radius, centre + RADIUS, centre + radius); The Bounds Mpaint.setcolor (mfirstcolor) used to define the shape and size of the arc; Sets the color of the ring for (int i = 0; i < MCount; i++) {Canvas.drawarc (oval, I * (ItemSize + msplitsize), ItemSize, False, Mpaint); Draw Arc}mpaint.setcolor (Msecondcolor) according to the progress; Sets the color of the ring for (int i = 0; i < Mcurrentcount; i++) {Canvas.drawarc (oval, I * (ItemSize + msplitsize), ItemSize, False, MP aint); Draw arcs based on progress}}

Here's what you need to be aware of:

Draw BLOCK: The proportion of each block is calculated based on the number of blocks and the gap.

Drawing: When the graph is larger, directly using the ring inner tangent square size constraints, when the picture is more than the hour, in the center of the position of the drawing. Some mathematical operation process, the landlord in the draft painting a meeting, not complicated, we draw pictures, I do not put a draft.

4. Join the Touch Monitor:

/** * Current number +1 */public void up () {mcurrentcount++;p ostinvalidate ();} /** * Current number-1 */public void Down () {mcurrentcount--;p ostinvalidate ();} private int Xdown, xUp; @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:xDown = (int) event.gety (); Break;case MotionEvent.ACTION_UP:xUp = (int) event.gety (); if (xUp > Xdown)//Decline {down ();} else{up ();} break;} return true;}
Touch monitoring is also very easy ha, basic can be achieved, we can also add a minimum distance acceleration or something, all line.


At last:


Unfortunately landlord is not find that volume figure, don't ask me to pick Tuha, just take a few pictures to try.


Source code click here to download


Hey, leave a message, top a ha ~



Android own definition view (iv) Video volume control

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.