欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现自定义小圆圈加载

程序员文章站 2022-05-04 20:01:30
...

首先我们先自定义一个类

package com.xxx.xjq.util;


import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class PerSon extends View {

    private Paint paint;
    private int mProgress = 0;
    private int mCountProgress = 0;
    private ValueAnimator animator;
    private int mColor;

    public PerSon(Context context) {
        super(context);
        init();
    }
    public PerSon(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public PerSon(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public void init(){
        paint = new Paint();
        paint.setAntiAlias(true);

        animator = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED,Color.YELLOW);
        animator.setDuration(3000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mColor = (int) animator.getAnimatedValue();
            }
        });
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        paint.setStrokeWidth(0);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(100,100,60,paint);
        canvas.drawCircle(100,100,40,paint);
        canvas.drawText(mCountProgress + "%",100,100,paint);
        paint.setStrokeWidth(20);
        RectF rectF = new RectF(50,50,150,150);
        canvas.drawArc(rectF,0,mProgress,false,paint);
    }
    public void setProgress(int progress) {
        mProgress = progress;
        mCountProgress = progress * 100 / 360;
        invalidate();
    }
}

在我们xml找到自定义的控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="#0f0"
    tools:context="com.xxx.xjq.MainActivity">

    <com.xxx.xjq.util.PerSon
        android:id="@+id/ps"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_gravity="center_horizontal"

        />
</LinearLayout>
然后在我们MainActivity开始实现小圆圈让它动起来

public class MainActivity extends AppCompatActivity {
    private PerSon ps;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        ps = (PerSon) findViewById(R.id.ps);
        //异步
        new AsyncTask<String,Integer,String>(){
            @Override
            protected String doInBackground(String... strings) {
                for (int i = 0; i < 360; i++) {
                    SystemClock.sleep(5);
                    publishProgress(i+1);
                }
                return null;
            }
            @Override
            protected void onProgressUpdate(Integer... values) {
                ps.setProgress(values[0]);
            }
        }.execute();
    }
    }