android自定义View之(四)------一键清除动画

1.前言:

    自己也是参考别人的一些自定义view例子,学习了一些基本的自定义view的方法。今天,我参考了一些资料,再结合自已的一些理解,做了一个一键清除的动画。当年,我实现这个是用了几张图片,采用Frame anination的方式来实现,但是这个方法,不灵活,并且占资源,下面,我就采用自定义view的方法来实现这个功能。

2.效果图:




3.具体详细代码

3.1 \res\values\attrs_on_key_clear_circle_view.xml

[html]  view plain  copy
  1. <resources>  
  2.     <declare-styleable name="OnKeyClearCircleView">  
  3.         <attr name="innerCircleColor" format="color" />  
  4.         <attr name="outCircleColor" format="color" />  
  5.         <attr name="innerCircleRadius" format="integer" />  
  6.         <attr name="progress" format="integer" />  
  7.         <attr name="textSize" format="dimension" />  
  8.         <attr name="outArcwidth" format="dimension" />  
  9.     declare-styleable>  
  10. resources>  


对属性的说明:

      一般,我们在res/values文件夹中添加attrs.xml文件来放置,其实,这个文件名并不一定要写成attrs.xml,但是按照安卓源码的写法并且也便于别人查看代码的时候明确这个文件的用意,还是写成attrs.xml。
      我们可以参看一下安卓的源码:打开源码文件夹下\frameworks\base\core\res\res\values\attrs.xml文件,我们会发现这里面定义了很多attr的标签,下面是我从这里面找到的一些:

[html]  view plain  copy
  1. <attr name="buttonBarStyle" format="reference" />  
  2. <attr name="cacheColorHint" format="color" />  
  3. <attr name="textColorHint" format="reference|color" />  
  4. <attr name="textIsSelectable" format="boolean" />  
  5. <attr name="scrollbarFadeDuration" format="integer" />  
  6. <attr name="alpha" format="float" />  
  7. <attr name="textSize" format="dimension" />  
  8. <attr name="columnDelay" format="float|fraction" />  
  9. <attr name="fontFamily" format="string" />  
  10.    
  11.       
  12.     <attr name="typeface">  
  13.         <enum name="normal" value="0" />  
  14.         <enum name="sans" value="1" />  
  15.         <enum name="serif" value="2" />  
  16.         <enum name="monospace" value="3" />  
  17.     attr>  
  18.    
  19.       
  20.     <attr name="textStyle">  
  21.         <flag name="normal" value="0" />  
  22.         <flag name="bold" value="1" />  
  23.         <flag name="italic" value="2" />  
  24.     attr>  

其中,format代表的是这条属性的值的类型: 
1.reference: 参考指定Theme中资源ID,这个类型意思就是你传的值可以是引用资源 
2.string:  字符 如果你想别人既能直接写值也可以用类似"@string/test"引用资源的方式,可以写成format="string|reference" 
3.Color: 颜色 
4.boolean:  布尔值 
5.dimension:  尺寸值 
6.float:  浮点型 
7.integer:  整型 
8.fraction:  百分数 
9.enum:   枚举 ,如果你提供的属性只能让别人选择,不能随便传入,
10.flag:   位或运算


写法样例:

[html]  view plain  copy
  1. <declare-styleable name="TextViewMultiLineBackgroundState">  
  2.           
  3.         <attr name="state_multiline" format="boolean" />  
  4.     declare-styleable>  

3.2 OnKeyClearCircleView.java

[java]  view plain  copy
  1. import android.content.Context;  
  2. import android.content.res.TypedArray;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Matrix;  
  8. import android.graphics.Paint;  
  9. import android.graphics.RectF;  
  10. import android.graphics.SweepGradient;  
  11. import android.util.AttributeSet;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import com.example.administrator.customview.R;  
  15.   
  16. public class OnKeyClearCircleView extends View implements Runnable{  
  17.   
  18.     private static final String TAG = "OnKeyClearCircleView";  
  19.     private Paint paint;  
  20.     private Paint outCirclePaint;  
  21.     private Paint textPaint;  
  22.     private Paint outArcPaint;  
  23.     private Paint radarPain;  
  24.     private Paint pointPain;  
  25.   
  26.     private int radarRotateDegree;  
  27.     private int innerCircleColor;  
  28.     private int innerCircleRadius;  
  29.     private int outCircleColor;  
  30.     private float outArcwidth;  
  31.   
  32.     private SweepGradient outArcSweepGradient;  
  33.     private SweepGradient radarSweepGradient;  
  34.   
  35.     private Bitmap pointDrawable;  
  36.     private Matrix pointRotate=new Matrix();  
  37.   
  38.     private int progress;  
  39.     private float textSize;  
  40.     private int padding;  
  41.   
  42.     private float startAngle;  
  43.     private float radarSweepAngle;  
  44.     private float pointRotateDegree;  
  45.   
  46.     private boolean isSart;  
  47.   
  48.     public OnKeyClearCircleView(Context context) {  
  49.         super(context);  
  50.         init(null0);  
  51.     }  
  52.   
  53.     public OnKeyClearCircleView(Context context, AttributeSet attrs) {  
  54.         super(context, attrs);  
  55.         init(attrs, 0);  
  56.     }  
  57.   
  58.     public OnKeyClearCircleView(Context context, AttributeSet attrs, int defStyle) {  
  59.         super(context, attrs, defStyle);  
  60.         init(attrs, defStyle);  
  61.     }  
  62.   
  63.     private void init(AttributeSet attrs, int defStyle) {  
  64.         final TypedArray a = getContext().obtainStyledAttributes(  
  65.                 attrs, R.styleable.OnKeyClearCircleView, defStyle, 0);  
  66.         innerCircleColor = a.getColor(R.styleable.OnKeyClearCircleView_innerCircleColor, Color.BLUE);  
  67.         outCircleColor = a.getColor(R.styleable.OnKeyClearCircleView_outCircleColor, Color.GRAY);  
  68.         innerCircleRadius = a.getInt(R.styleable.OnKeyClearCircleView_innerCircleRadius, 10);  
  69.         progress = a.getInt(R.styleable.OnKeyClearCircleView_progress,0);  
  70.         textSize = a.getDimension(R.styleable.OnKeyClearCircleView_textSize, 20);  
  71.         outArcwidth = a.getDimension(R.styleable.OnKeyClearCircleView_outArcwidth, 20);  
  72.         a.recycle();  
  73.         pointDrawable = BitmapFactory.decodeResource(getResources(),R.drawable.point);  
  74.         isSart = false;  
  75.         startAngle = 0;  
  76.         radarRotateDegree = 0;  
  77.         radarSweepAngle = 0;  
  78.         pointRotateDegree = 0;  
  79.         padding = 5;  
  80.         paint = new Paint();  
  81.         paint.setAntiAlias(true);  
  82.         paint.setColor(innerCircleColor);  
  83.         outCirclePaint = new Paint();  
  84.         outCirclePaint.setAntiAlias(true);  
  85.         outCirclePaint.setColor(outCircleColor);  
  86.         outCirclePaint.setStyle(Paint.Style.FILL);  
  87.         textPaint = new Paint();  
  88.         textPaint.setTextSize(textSize);  
  89.         textPaint.setAntiAlias(true);  
  90.         outArcPaint = new Paint();  
  91.         outArcPaint.setAntiAlias(true);  
  92.         outArcPaint.setStyle(Paint.Style.STROKE);  
  93.         outArcPaint.setStrokeWidth(outArcwidth);  
  94.         outArcPaint.setStrokeCap(Paint.Cap.ROUND);  
  95.         radarPain = new Paint();  
  96.         outArcPaint.setAntiAlias(true);  
  97.         pointPain = new Paint();  
  98.         pointPain.setAntiAlias(true);  
  99.         Thread thread=new Thread(this);  
  100.         thread.start();  
  101.     }  
  102.   
  103.     @Override  
  104.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  105.         //super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  106.         int width = MeasureSpec.getSize(widthMeasureSpec);  
  107.         int height = MeasureSpec.getSize(heightMeasureSpec);  
  108.         int d = (width >= height) ? height : width;  
  109.         setMeasuredDimension(d,d);  
  110.     }  
  111.   
  112.     @Override  
  113.     protected void onDraw(Canvas canvas) {  
  114.   
  115.         int width = getMeasuredWidth();  
  116.         int height = getMeasuredHeight();  
  117.         int pointX =  width/2;  
  118.         int pointY = height/2;  
  119.         RectF rectf = new RectF(outArcwidth/2,outArcwidth/2,width-outArcwidth/2,height-outArcwidth/2);  
  120.         //outArcSweepGradient = new SweepGradient(0,0,getResources().getColor(R.color.start_color),getResources().getColor(R.color.end_color));  
  121.         outArcSweepGradient = new SweepGradient(pointX,pointY,Color.WHITE,Color.WHITE);  
  122.         outArcPaint.setShader(outArcSweepGradient);  
  123.         canvas.drawArc(rectf,startAngle,180,false,outArcPaint);  
  124.   
  125.         canvas.drawCircle(pointX,pointY,pointX -outArcwidth-padding,outCirclePaint);  
  126.   
  127.         if(radarSweepAngle < 180){  
  128.             radarSweepGradient = new SweepGradient(pointX,pointY,Color.WHITE,Color.RED);  
  129.         }else{  
  130.             radarSweepGradient = new SweepGradient(pointX,pointY,Color.WHITE,Color.GREEN);  
  131.         }  
  132.         radarPain.setShader(radarSweepGradient);  
  133.         RectF rectfRadar = new RectF(outArcwidth+padding,outArcwidth+padding,width-outArcwidth-padding,height-outArcwidth-padding);  
  134.         canvas.drawArc(rectfRadar,0,radarSweepAngle,true,radarPain);  
  135.   
  136.         canvas.save();  
  137.         canvas.translate(pointX,pointY);  
  138.         pointRotate.setRotate(pointRotateDegree);  
  139.         canvas.drawBitmap(pointDrawable, pointRotate, pointPain);  
  140.         canvas.restore();;  
  141.   
  142.         canvas.drawCircle(pointX,pointY,innerCircleRadius,paint);  
  143.   
  144.         float textWidth = textPaint.measureText(progress + "%");  
  145.         if(progress < 50){  
  146.             //textPaint.setColor(oxbf3800);  
  147.             textPaint.setColor(Color.RED);  
  148.         }else{  
  149.             //textPaint.setColor(new Color(ox6ec705));  
  150.             textPaint.setColor(Color.GREEN);  
  151.         }  
  152.         canvas.drawText(progress+"%",pointX - textWidth/2,pointY + textSize/2 ,textPaint);  
  153.     }  
  154.   
  155.     @Override  
  156.     public void run() {  
  157.             while(true){  
  158.                 if(isSart){  
  159.                     this.startAngle += 20;  
  160.                     if(this.startAngle > 360){  
  161.                         this.startAngle = this.startAngle-360;  
  162.                     }  
  163.                     this.radarSweepAngle += 10;  
  164.                     if(this.radarSweepAngle > 360){  
  165.                         this.radarSweepAngle = this.radarSweepAngle-360;  
  166.                     }  
  167.                     this.pointRotateDegree += 10;  
  168.                     if(this.pointRotateDegree > 360){  
  169.                         this.pointRotateDegree = this.pointRotateDegree-360;  
  170.                     }  
  171.                     progress = (int)radarSweepAngle*100/360;  
  172.   
  173.                     postInvalidate();  
  174.                     try {  
  175.                         Thread.sleep(50);  
  176.                     } catch (InterruptedException e) {  
  177.                         e.printStackTrace();  
  178.                     }  
  179.                 }  
  180.             }  
  181.     }  
  182.   
  183.     public void startClear(){  
  184.         this.isSart =  true;  
  185.     }  
  186.   
  187.     public void stopClear(){  
  188.         this.isSart =false;  
  189.     }  
  190.   
  191.     public int getProgress() {  
  192.         return progress;  
  193.     }  
  194.   
  195.     public void setProgress(int progress) {  
  196.         this.progress = progress;  
  197.     }  
  198. }  

3.3 res\layout\activity_custom_view_activity4.xml

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context="com.example.administrator.customview.customview04.CustomViewActivity4">  
  10.   
  11.     <com.example.administrator.customview.customview04.OnKeyClearCircleView  
  12.         android:layout_width="200dp"  
  13.         android:layout_height="200dp"  
  14.         android:id="@+id/onKeyClearCircleView"  
  15.         android:background="#fdda6f"  
  16.         app:innerCircleColor="#ffffff"  
  17.         app:innerCircleRadius="35"  
  18.         app:textSize="20sp"  
  19.         app:progress="82"  
  20.         app:outCircleColor="#dbdad7"  
  21.         app:outArcwidth="10dp"  
  22.     />  
  23.   
  24.     <Button  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="start"  
  28.         android:id="@+id/start"  
  29.         android:layout_below="@+id/onKeyClearCircleView"  
  30.         android:layout_alignParentLeft="true"  
  31.         android:layout_alignParentStart="true" />  
  32.   
  33.     <Button  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="end"  
  37.         android:id="@+id/end"  
  38.         android:layout_below="@+id/start"  
  39.         android:layout_alignParentLeft="true"  
  40.         android:layout_alignParentStart="true" />  
  41.   
  42. RelativeLayout>  

3.4 CustomViewActivity4.java

[java]  view plain  copy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.widget.Button;  
  5. import com.example.administrator.customview.R;  
  6.   
  7. public class CustomViewActivity4 extends Activity implements View.OnClickListener {  
  8.   
  9.     private OnKeyClearCircleView onKeyClearCircleView;  
  10.     private Button start;  
  11.     private Button end;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_custom_view_activity4);  
  17.         init();  
  18.     }  
  19.   
  20.     private void init() {  
  21.         onKeyClearCircleView = (OnKeyClearCircleView) findViewById(R.id.onKeyClearCircleView);  
  22.         start = (Button) findViewById(R.id.start);  
  23.         start.setOnClickListener(this);  
  24.         end = (Button) findViewById(R.id.end);  
  25.         end.setOnClickListener(this);  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onClick(View view) {  
  30.         if(view == start){  
  31.             onKeyClearCircleView.startClear();  
  32.         }else if(view == end){  
  33.             onKeyClearCircleView.stopClear();  
  34.         }  
  35.     }  
  36. }  


4.代码下载地址:



5.参考资料:

1.Android自定义View实现雷达扫描动画

http://blog.csdn.net/oxygen0106/article/details/40144781

2.利用Android自定义View实现转盘旋转的效果

http://blog.csdn.net/canchew/article/details/5633421


你可能感兴趣的:(#,自定义view)