Android Menu Arrow Toggle Control

Source: Internet
Author: User
Tags getcolor

There's a very appealing way to switch between menu buttons and arrows on the android material design.

Two days ago I saw an app using this effect. Take this control and learn it.

1  PackageCom.example.drawablearrow;2 3 4 ImportAndroid.content.Context;5 ImportAndroid.graphics.Canvas;6 ImportAndroid.graphics.ColorFilter;7 ImportAndroid.graphics.Paint;8 ImportAndroid.graphics.Path;9 ImportAndroid.graphics.PixelFormat;Ten ImportAndroid.graphics.Rect; One Importandroid.graphics.drawable.Drawable; A  -  - //This class is the class used to implement the menu programming arrow effect. the  Public Abstract classDrawerarrowdrawableextendsdrawable { -     Private Static Final floatArrow_head_angle = (float) Math.toradians (45.0D);//angle of the arrow -     protected floatMbargap;//the interval between two lines of the menu button -     protected floatMbarsize;//Length of the menu button line +     protected floatmbarthickness;//width of the menu button line -     protected floatMmiddlearrowsize;//The length of the line in the middle of the arrow button +     protected FinalPaint Mpaint =NewPaint (); A     protected FinalPath MPath =NewPath (); at     protected floatmprogress; -     protected intmsize; -     protected floatMverticalmirror =1f; -     protected floatMtopbottomarrowsize;//The length of the upper and lower part of the arrow button -     protectedcontext context; -  in      Publicdrawerarrowdrawable (Context context) { -          This. Context =context; to          This. Mpaint.setantialias (true);//set anti-aliasing +          This. Mpaint.setcolor (Context.getresources (). GetColor (R.color.ldrawer_color));//set the color of the brush to white -          This. msize =context.getresources (). Getdimensionpixelsize (r.dimen.ldrawer_drawablesize); the          This. mbarsize =context.getresources (). Getdimensionpixelsize (r.dimen.ldrawer_barsize); *          This. mtopbottomarrowsize =context.getresources (). Getdimensionpixelsize (r.dimen.ldrawer_topbottombararrowsize); $          This. mbarthickness =context.getresources (). Getdimensionpixelsize (r.dimen.ldrawer_thickness);Panax Notoginseng          This. Mbargap =context.getresources (). Getdimensionpixelsize (r.dimen.ldrawer_gapbetweenbars); -          This. mmiddlearrowsize =context.getresources (). Getdimensionpixelsize (r.dimen.ldrawer_middlebararrowsize); the          This. Mpaint.setstyle (Paint.Style.STROKE); +         //set up the intersection of two lines at the intersection of the processing A          This. Mpaint.setstrokejoin (Paint.Join.ROUND); the          This. Mpaint.setstrokecap (Paint.Cap.SQUARE); +         //set the width of a brush line -          This. Mpaint.setstrokewidth ( This. mbarthickness); $     } $     //This method is used for animation conversion when the third parameter of the difference is the progress -     protected floatLerp (floatPARAMFLOAT1,floatPARAMFLOAT2,floatPARAMFLOAT3) { -         returnPARAMFLOAT1 + PARAMFLOAT3 * (PARAMFLOAT2-paramFloat1); the     } - Wuyi      Public voidDraw (canvas canvas) { theRect Localrect =getbounds (); -         floatF1 = Lerp ( This. Mbarsize, This. Mtopbottomarrowsize, This. mprogress); Wu         floatF2 = Lerp ( This. Mbarsize, This. Mmiddlearrowsize, This. mprogress); -         floatF3 = Lerp (0.0F, This. mbarthickness/2.0f, This. mprogress); About         floatF4 = Lerp (0.0F, Arrow_head_angle, This. mprogress); $         floatF5 = 0.0F; -         floatF6 = 180.0F; -         floatF7 = Lerp (F5, F6, This. mprogress); -         floatF8 = Lerp ( This. Mbargap + This. mbarthickness, 0.0F, This. mprogress); A          This. Mpath.rewind (); +         floatF9 =-f2/2.0f; the          This. Mpath.moveto (F9 + F3, 0.0F); -          This. Mpath.rlineto (F2-F3, 0.0F); $         floatF10 = (float) Math.Round (F1 *Math.Cos (f4)); the         floatF11 = (float) Math.Round (F1 *Math.sin (f4)); the          This. Mpath.moveto (F9, F8); the          This. Mpath.rlineto (F10, F11); the          This. Mpath.moveto (F9,-f8); -          This. Mpath.rlineto (F10,-F11); in          This. Mpath.moveto (0.0F, 0.0F); the          This. Mpath.close (); the Canvas.save (); About         if(!Islayoutrtl ()) theCanvas.rotate (180.0F, Localrect.centerx (), Localrect.centery ()); theCanvas.rotate (F7 *Mverticalmirror, Localrect.centerx (), Localrect.centery ()); the canvas.translate (Localrect.centerx (), Localrect.centery ()); +Canvas.drawpath ( This. MPath, This. Mpaint); - Canvas.restore (); the     }Bayi  the      Public intgetintrinsicheight () { the         return  This. msize; -     } -  the      Public intgetintrinsicwidth () { the         return  This. msize; the     } the  -      Public voidSetalpha (intAlpha) { the          This. Mpaint.setalpha (Alpha); the     } the 94 @Override the      Public intgetopacity () { the         returnpixelformat.translucent; the     }98  About      Public Abstract BooleanIslayoutrtl (); - 101      Public voidSetcolorfilter (Colorfilter colorfilter) {102          This. Mpaint.setcolorfilter (colorfilter);103     }104  the      Public voidSetverticalmirror (Booleanmverticalmirror) {106          This. Mverticalmirror = Mverticalmirror? 1:-1;107     }108 109      Public voidSetprogress (floatparamfloat) { the          This. mprogress =paramfloat;111 invalidateself (); the     }113  the      Public voidSetColor (intresourceId) { the          This. Mpaint.setcolor (Context.getresources (). GetColor (ResourceId)); the     }117}

Using this class is primarily a call to his two methods Setprogress and Setverticalmirror the first method is to set the progress of the animation through the continuous modification of the progress to achieve the effect. The second method is used to set the direction of rotation.

Android Menu Arrow Toggle 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.