圆环加属性动画

来源:互联网 发布:网络用语小白什么意思 编辑:程序博客网 时间:2024/05/29 09:15
//布局
<?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"    xmlns:app="http://schemas.android.com/apk/res-auto"    tools:context="test.bwei.com.zhoukao1009.MainActivity">    <test.bwei.com.zhoukao1009.MyRound        android:id="@+id/mpr"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:ringSize="10dp"        app:progressColor="#883399"        app:radiuSize="150dp"        app:textSize="20sp"        />    <Button        android:id="@+id/begin"        android:layout_alignParentBottom="true"        android:layout_width="wrap_content"        android:layout_alignParentLeft="true"        android:text="开始"        android:textSize="25sp"        android:layout_height="50dp" />    <Button        android:id="@+id/resut"        android:layout_alignParentBottom="true"        android:layout_width="wrap_content"        android:textSize="25sp"        android:layout_alignParentRight="true"        android:text="重置"        android:layout_height="50dp" /></RelativeLayout>
 

//   MyMainActivity

public class MainActivity extends AppCompatActivity {     MyRound mpr;    Button began,resut;    boolean flag=false;    Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            mpr.setmCountProgress();        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mpr= (MyRound) findViewById(R.id.mpr);        began= (Button) findViewById(R.id.begin);        resut= (Button) findViewById(R.id.resut);        //左右移动        float x=mpr.getTranslationX();        ObjectAnimator animator=ObjectAnimator.ofFloat(mpr,"translationX",x,50,x);        animator.setDuration(5000);        //缩放        ObjectAnimator animator2=ObjectAnimator.ofFloat(mpr,"scaleY",1f,0.5f,2f,1f);        animator2.setDuration(5000);        AnimatorSet set=new AnimatorSet();        set.play(animator).after(animator2);        set.start();        began.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                new AsyncTask<String,Integer,String>(){                    @Override                    protected String doInBackground(String... strings) {                        flag=false;                        for (int i = 1; i < 361; i++) {                            if(flag)                            {                                handler.sendEmptyMessage(0);                                break;                            }                            SystemClock.sleep(10);                            publishProgress(i);                        }                        return null;                    }                    @Override                    protected void onProgressUpdate(Integer... values) {                        mpr.setmProgress(values[0]);                    }                }.execute();            }        });         resut.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                flag=true;             }         });    }
   //MyRound

public class MyRound extends View {    Paint paint;    private int mProgress=0;    private int mCountProgress=0;    private float mRadiuSize=0;    private float mRingSize=0;    private float mTextSize=0;    private int mProgressColor=0;    public MyRound(Context context) {        super(context);        init();    }    public MyRound(Context context, AttributeSet attrs) {        super(context, attrs);        getCustomAttr(context,attrs);        init();    }    private void getCustomAttr(Context context, AttributeSet attrs) {        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.MyProgressRound);        mRadiuSize=array.getDimension(R.styleable.MyProgressRound_radiuSize,100);        mRingSize=array.getDimension(R.styleable.MyProgressRound_ringSize,10);        mTextSize=array.getDimension(R.styleable.MyProgressRound_textSize,10);        mProgressColor=array.getColor(R.styleable.MyProgressRound_progressColor,Color.BLACK);    }    public MyRound(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    public void init(){        paint=new Paint();        paint.setAntiAlias(true);        paint.setTextSize(45);    }    @Override    protected void onDraw(Canvas canvas) {        paint.setStrokeWidth(0);        paint.setColor(Color.GREEN);        paint.setStyle(Paint.Style.STROKE);        canvas.drawCircle(200,200,mRadiuSize,paint);        canvas.drawCircle(200,200,mRadiuSize-mRingSize,paint);        canvas.drawText(mCountProgress+"%",200,200,paint);        RectF rectF=new RectF(200-mRadiuSize+mRingSize/2,200-mRadiuSize+mRingSize/2,200+mRadiuSize-mRingSize/2,200+mRadiuSize-mRingSize/2);        paint.setStrokeWidth(mRingSize);        canvas.drawArc(rectF,0,mProgress,false,paint);    }    public void setmProgress(int progress){        mProgress=progress;        mCountProgress=progress*100/360;        invalidate();    }    public void setmCountProgress(){        mProgress=0;        mCountProgress=0;        invalidate();    }