Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android自定義View之(四)------一鍵清除動畫

android自定義View之(四)------一鍵清除動畫

編輯:關於Android編程

1.前言:

自己也是參考別人的一些自定義view例子,學習了一些基本的自定義view的方法。今天,我參考了一些資料,再結合自已的一些理解,做了一個一鍵清除的動畫。當年,我實現這個是用了幾張圖片,采用Frame anination的方式來實現,但是這個方法,不靈活,並且占資源,下面,我就采用自定義view的方法來實現這個功能。

2.效果圖:

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083972.gif

 

 

3.具體詳細代碼

3.1 es aluesattrs_on_key_clear_circle_view.xml

 


    

 

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{

    private 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, defStyle);
        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(widthMeasureSpec, 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 < 180){
            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 < 50){
            //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.startAngle += 20;
                    if(this.startAngle > 360){
                        this.startAngle = this.startAngle-360;
                    }
                    this.radarSweepAngle += 10;
                    if(this.radarSweepAngle > 360){
                        this.radarSweepAngle = this.radarSweepAngle-360;
                    }
                    this.pointRotateDegree += 10;
                    if(this.pointRotateDegree > 360){
                        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 reslayoutactivity_custom_view_activity4.xml

 

 



    

 

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();
        }
    }
}


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved