属性动画+自定义view进度圆环的倒计时

来源:互联网 发布:html设计软件 编辑:程序博客网 时间:2024/05/30 04:33

 属性动画+自定义view进度圆环的倒计时

 

  MainActivity
public class MainActivity extends AppCompatActivity { private ImageView iv; private Handler handler; private MyView myView;   int item=3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    iv = (ImageView) findViewById(R.id.iv);    myView= (MyView) findViewById(R.id.myView); //动画的方法 initAnimator(); } //属性动画 private void initAnimator() { AnimatorSet set = new AnimatorSet(); float y = iv.getTranslationY();   ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv,"translationY",y,450);   objectAnimator.setDuration(3000); ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv,"scaleY",1f,2f,1f);   objectAnimator1.setDuration(3000);   set.play(objectAnimator).before(objectAnimator1); set.start();  //倒计时跳转 handler = new Handler(); handler.postDelayed(new Runnable() {   @Override public void run() { item--; if (item==1){ Intent intent=new Intent(MainActivity.this,ShowActivity.class); startActivity(intent);   }   handler.postDelayed(this,2000); } },2000);   new AsyncTask<String,Integer,String>(){   @Override protected String doInBackground(String... strings) { for (int i = 1; i <=4 ; i++) {   SystemClock.sleep(1000);   publishProgress(i); } return null;  }   @Override   protected void onProgressUpdate(Integer... values) {   myView.setProgress(values[0]);  } }.execute();   }}

自定义view

package com.example.myviewhandler;import android.animation.ArgbEvaluator;import android.animation.ValueAnimator;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * 自定义View */public class MyView extends View {    private Paint paint;    boolean flag = true;    private int mProgress = 0;    private int mCountProgress = 0;    private float mRadiuSize = 0;    private float mRingSize = 0;    private float mTextSize = 0;    private int mProgressColor = 0;    private int currentColor;    private ValueAnimator animator;    public MyView(Context context) {        super(context);        init();    }    //自定义方法    private void init() {        //画笔        paint=new Paint();        paint.setAntiAlias(true);    }    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);        //调用下面定义的一个方法        getCustomAttr(context,attrs);        init();    }    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        getCustomAttr(context,attrs);        init();    }    //实现测量的方法    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthmode = MeasureSpec.getMode(widthMeasureSpec);        int widthsize = MeasureSpec.getSize(widthMeasureSpec);        int hightmode = MeasureSpec.getMode(heightMeasureSpec);        int hightsize = MeasureSpec.getSize(heightMeasureSpec);        int width=0;        int hight=0;        if (widthmode==MeasureSpec.AT_MOST){            width = (int) (mRadiuSize *2);        }else{            width= Math.max(widthsize,(int)(mRadiuSize*2));        }        if (hightmode==MeasureSpec.AT_MOST){            hight = (int) (mRadiuSize * 2);        }else{            hight=  Math.max(hightsize,(int)(mRadiuSize*2));        }        setMeasuredDimension(width,hight);    }    //自定义的一个方法    private void getCustomAttr(Context context, AttributeSet attrs) {        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);        mRadiuSize = array.getDimension(R.styleable.MyView_radiuSize, 100);        mRingSize = array.getDimension(R.styleable.MyView_ringSize, 10);        mTextSize = array.getDimension(R.styleable.MyView_textSize, 10);        mProgressColor = array.getColor(R.styleable.MyView_progressColor, currentColor);    }    @Override    protected void onDraw(Canvas canvas) {        //  super.onDraw(canvas);        animator = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);        animator.setDuration(500);        animator.setRepeatCount(ValueAnimator.INFINITE);        animator.setRepeatMode(ValueAnimator.REVERSE);        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator valueAnimator) {                currentColor = (int) valueAnimator.getAnimatedValue();            }        });        paint.setStrokeWidth(0);        paint.setColor(Color.BLACK);        paint.setStyle(Paint.Style.STROKE);        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, mRadiuSize, paint);        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, mRadiuSize - mRingSize, paint);        paint.setTextSize(mTextSize);        String text = mCountProgress+"";        float textWidth = paint.measureText(text);        canvas.drawText(text, getMeasuredWidth() / 2 - textWidth / 2, getMeasuredWidth() / 2 + mTextSize / 2, paint);        RectF rectF = new RectF(getMeasuredWidth() / 2 - mRadiuSize + mRingSize / 2, getMeasuredHeight() / 2 - mRadiuSize + mRingSize / 2, getMeasuredWidth() / 2 + mRadiuSize - mRingSize / 2, getMeasuredHeight() / 2 + mRadiuSize - mRingSize / 2);        paint.setStrokeWidth(mRingSize);        paint.setColor(currentColor);        canvas.drawArc(rectF, 0, mProgress*90, false, paint);    }    public void setProgress(int progress) {        if (flag) {            animator.start();            flag = false;        }        mProgress = (4-progress);        mCountProgress = (4-progress) ;        invalidate();    }}

跳转的第二个页面

package com.example.myviewhandler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class ShowActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_show);    }}

要在values里创建attrs文件

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyView">        <attr name="progressColor" format="color"/>        <attr name="textSize" format="dimension"/>        <attr name="ringSize" format="dimension"/>        <attr name="radiuSize" format="dimension"/>    </declare-styleable></resources>

布局文件

mainactivity

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"     >    <com.example.myviewhandler.MyView        android:id="@+id/myView"        android:layout_width="100dp"        android:layout_height="100dp"        app:ringSize="10dp"        app:progressColor="#ff93"        app:textSize="20sp"        app:radiuSize="50dp"        >    </com.example.myviewhandler.MyView>    <ImageView        android:id="@+id/iv"        android:layout_width="200dp"        android:layout_height="200dp"        android:src="@mipmap/y"        android:scaleType="fitXY"        android:layout_centerHorizontal="true"        /></RelativeLayout>

showactivity 布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.myviewhandler.ShowActivity">   <TextView       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="展示页面"       /></RelativeLayout>
 
原创粉丝点击