1. Preface:
I also learn some basic custom view methods by referring to some custom view examples from others. Today, I reference some information, and then combined with some of my own understanding, did a one-click Erase animation. That year, I realized this is a few pictures, using frame anination way to achieve, but this method, not flexible, and resources, below, I will use a custom view method to achieve this function.
2.:
3. Specific detailed Code 3.1 \res\values\attrs_on_key_clear_circle_view.xml
<resources> <declare-styleable name= "Onkeyclearcircleview" > <attr name= "Innercirclecolor" format= "Color"/> <attr name= "outcirclecolor" format= "color"/> <attr name= "Innercircleradius" format= "integer"/> <attr name= "Progress" format= "integer"/> <attr name= "textSize" format= " Dimension "/> <attr name=" Outarcwidth "format=" Dimension "/> </declare-styleable></ Resources>
3.2 Onkeyclearcircleview.java
Import Android.content.context;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.rectf;import Android.graphics.sweepgradient;import Android.util.attributeset;import Android.util.log;import Android.view.View; Import Com.example.administrator.customview.r;public class Onkeyclearcircleview extends View implements runnable{Priv Ate static final String TAG = "Onkeyclearcircleview"; private paint paint; Private Paint Outcirclepaint; Private Paint Textpaint; Private Paint Outarcpaint; Private Paint Radarpain; Private Paint Pointpain; private int radarrotatedegree; private int innercirclecolor; private int Innercircleradius; private int outcirclecolor; private float outarcwidth; Private Sweepgradient outarcsweepgradient; Private Sweepgradient RADARSWEEPGRadient; Private Bitmap pointdrawable; Private matrix pointrotate=new matrix (); private int progress; private float textSize; private int padding; private float startangle; private float Radarsweepangle; private float Pointrotatedegree; Private Boolean Issart; Public Onkeyclearcircleview (Context context) {super (context); init (null, 0); } public Onkeyclearcircleview (context context, AttributeSet Attrs) {Super (context, attrs); Init (attrs, 0); } public Onkeyclearcircleview (context context, AttributeSet attrs, int defstyle) {Super (context, Attrs, defstyl e); Init (attrs, Defstyle); } private void Init (AttributeSet attrs, int defstyle) {final TypedArray a = GetContext (). obtainstyledattributes (Attrs, R.styleable.onkeyclearcircleview, Defstyle, 0); Innercirclecolor = A.getcolor (R.styleable.onkeyclearcircleview_innercirclecolor, Color.BLUE); Outcirclecolor = A.getcolor (R.styleable.onkeyclearcircleview_outcirclecolor, color.gray); Innercircleradius = A.getint (R.styleable.onkeyclearcircleview_innercircleradius, 10); progress = A.getint (r.styleable.onkeyclearcircleview_progress,0); TextSize = A.getdimension (r.styleable.onkeyclearcircleview_textsize, 20); Outarcwidth = A.getdimension (R.styleable.onkeyclearcircleview_outarcwidth, 20); A.recycle (); pointdrawable = Bitmapfactory.decoderesource (Getresources (), r.drawable.point); Issart = false; startangle = 0; Radarrotatedegree = 0; Radarsweepangle = 0; Pointrotatedegree = 0; padding = 5; Paint = new paint (); Paint.setantialias (TRUE); Paint.setcolor (Innercirclecolor); Outcirclepaint = new Paint (); Outcirclepaint.setantialias (TRUE); Outcirclepaint.setcolor (Outcirclecolor); Outcirclepaint.setstyle (Paint.Style.FILL); Textpaint = new Paint (); Textpaint.settextsize (tExtsize); Textpaint.setantialias (TRUE); Outarcpaint = new Paint (); Outarcpaint.setantialias (TRUE); Outarcpaint.setstyle (Paint.Style.STROKE); Outarcpaint.setstrokewidth (Outarcwidth); Outarcpaint.setstrokecap (Paint.Cap.ROUND); Radarpain = new Paint (); Outarcpaint.setantialias (TRUE); Pointpain = new Paint (); Pointpain.setantialias (TRUE); Thread Thread=new thread (this); Thread.Start (); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//super.onmeasure (Widthme Asurespec, Heightmeasurespec); int width = measurespec.getsize (widthmeasurespec); int height = measurespec.getsize (heightmeasurespec); int d = (width >= height)? Height:width; Setmeasureddimension (D,D); } @Override protected void OnDraw (canvas canvas) {int width = getmeasuredwidth (); int height = getmeasuredheight (); int pointx = WIDTH/2; int pointy = HEIGHT/2; RECTF RECTF = new RECTF (OUTARCWIDTH/2,OUTARCWIDTH/2,WIDTH-OUTARCWIDTH/2,HEIGHT-OUTARCWIDTH/2); Outarcsweepgradient = new Sweepgradient (0,0,getresources (). GetColor (R.color.start_color), Getresources (). GetColor (R.color.end_color)); Outarcsweepgradient = new Sweepgradient (pointx,pointy,color.white,color.white); Outarcpaint.setshader (outarcsweepgradient); Canvas.drawarc (Rectf,startangle,180,false,outarcpaint); Canvas.drawcircle (Pointx,pointy,pointx-outarcwidth-padding,outcirclepaint); if (Radarsweepangle <) {radarsweepgradient = new sweepgradient (pointx,pointy,color.white,color.red); }else{radarsweepgradient = new Sweepgradient (pointx,pointy,color.white,color.green); } radarpain.setshader (Radarsweepgradient); RECTF Rectfradar = new RECTF (outarcwidth+padding,outarcwidth+padding,width-outarcwidth-padding, height-outarcwidth-padding); Canvas.drawarc (Rectfradar,0,radarsweepangle,true,radarpain); Canvas.save (); Canvas.translate (Pointx,pointy); Pointrotate.setrotate (Pointrotatedegree); Canvas.drawbitmap (pointdrawable, Pointrotate, Pointpain); Canvas.restore (); Canvas.drawcircle (Pointx,pointy,innercircleradius,paint); float textWidth = Textpaint.measuretext (progress + "%"); if (Progress <) {//textpaint.setcolor (oxbf3800); Textpaint.setcolor (color.red); }else{//textpaint.setcolor (New Color (ox6ec705)); Textpaint.setcolor (Color.green); } canvas.drawtext (progress+ "%", Pointx-textwidth/2,pointy + TEXTSIZE/2, textpaint); } @Override public void Run () {while (true) {if (Issart) {This.startangl E + = 20; if (This.startangle >) {this.startangle = this.startangle-360; } this.radarsweEpangle + = 10; if (This.radarsweepangle >) {this.radarsweepangle = this.radarsweepangle-360; } This.pointrotatedegree + = 10; if (This.pointrotatedegree >) {this.pointrotatedegree = this.pointrotatedegree-360; } progress = (int) radarsweepangle*100/360; Postinvalidate (); try {thread.sleep (50); } catch (Interruptedexception e) {e.printstacktrace (); }}}} public void Startclear () {This.issart = true; } public void Stopclear () {This.issart =false; } public int getprogress () {return progress; } public void setprogress (int progress) {this.progress = progress; }}
3.3 Res\layout\activity_custom_view_activity4.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "xmlns:app=" Http://schemas.android.com/apk/res-auto "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:paddingleft=" @dimen/activity_horizontal_margin "Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android: paddingbottom= "@dimen/activity_vertical_margin" tools:context= " Com.example.administrator.customview.customview04.CustomViewActivity4 "> < Com.example.administrator.customview.customview04.OnKeyClearCircleView android:layout_width= "200DP" Android: layout_height= "200DP" android:id= "@+id/onkeyclearcircleview" android:background= "#fdda6f" App:innerci Rclecolor= "#ffffff" app:innercircleradius= "20sp" app:progress= "app:textsize=" App:outci Rclecolor= "#dbdad7" App:outarcwidtH= "10DP"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Start" android:id= "@+id/start" android:layout_below= "@+id/onkeyclearcircleview" and Roid:layout_alignparentleft= "true" android:layout_alignparentstart= "true"/> <button android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "End" android:id= "@+id/end" android:layout_below= "@+id/start" android:layout_alignparentleft= "true" Android:layout_alignparentstar T= "true"/></relativelayout>
3.4 Customviewactivity4.java
Import Android.app.activity;import android.os.bundle;import Android.view.view;import Android.widget.Button;import Com.example.administrator.customview.r;public class CustomViewActivity4 extends Activity implements View.onclicklistener {private Onkeyclearcircleview onkeyclearcircleview; Private Button start; Private Button end; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_CUSTOM_VIEW_ACTIVITY4); Init (); } private void Init () {Onkeyclearcircleview = (Onkeyclearcircleview) Findviewbyid (R.id.onkeyclearcircleview); Start = (Button) Findviewbyid (R.id.start); Start.setonclicklistener (this); End = (Button) Findviewbyid (r.id.end); End.setonclicklistener (this); @Override public void OnClick (view view) {if (view = = start) {onkeyclearcircleview.startclear (); }else if (view = = end) {ONKEYCLEarcircleview.stopclear (); } }}
4. Code:
5. References:
1.Android Custom view for radar scan animations
http://blog.csdn.net/oxygen0106/article/details/40144781
2. Use Android to customize the view to achieve the effect of turntable rotation
http://blog.csdn.net/canchew/article/details/5633421
Android Custom View (iv)------One-click Erase animation