自定义View实现圆形进度条跳转页面

效果:
在这里插入图片描述

//首先在values文件夹下创建一个attrs.xml:
在这里插入图片描述
?xml version=“1.0” encoding=“utf-8”?>












//布局:

<?xml version="1.0" encoding="utf-8"?>



<com.bwie.animator.AddView
android:id="@+id/MyCircleProgress"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“200dp”
app:r=“85”
app:strokeWidth=“5”
app:bgColor="#cccccc"
app:fgColor="#ff0000"
app:drawStyle=“STROKE”
/>

//自定义属性view
public class AddView extends View {
public int progress = 0;//进度实际值,当前进度
/**
* 自定义控件属性,可灵活的设置圆形进度条的大小、颜色、类型等
*/
private int mR;//圆半径,决定圆大小
private int bgColor;//圆或弧的背景颜色
private int fgColor;//圆或弧的前景颜色,即绘制时的颜色
private int drawStyle; //绘制类型 FILL画圆形进度条,STROKE绘制弧形进度条
private int strokeWidth;//STROKE绘制弧形的弧线的宽度
private int max = 100;//最大值,设置进度的最大值
private Paint mPaint;
private boolean opt;

public AddView(Context context) {
    this(context, null);
}

public AddView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);

}

public AddView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
    mR = tArray.getInteger(R.styleable.CircleProgressBar_r, 30);
    bgColor = tArray.getColor(R.styleable.CircleProgressBar_bgColor, Color.GRAY);
    fgColor = tArray.getColor(R.styleable.CircleProgressBar_fgColor, Color.RED);
    drawStyle = tArray.getInt(R.styleable.CircleProgressBar_drawStyle, 0);
    strokeWidth = tArray.getInteger(R.styleable.CircleProgressBar_strokeWidth, 10);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    //mPaint.setStyle(Paint.Style.STROKE);
    initProperty(attrs);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int center = getWidth() / 2; // 圆心位置
    int height = getHeight();
    mPaint.setColor(bgColor);
    mPaint.setStrokeWidth(strokeWidth);
    canvas.drawCircle(center, center, mR, mPaint);
    // 绘制圆环
    mPaint.setColor(fgColor);
    if (drawStyle == 0) {
        mPaint.setStyle(Paint.Style.STROKE);
        opt = false;
    } else {
        mPaint.setStyle(Paint.Style.STROKE);
        opt = true;
    }
    int top = (center - mR);
    int bottom = (center + mR);
    RectF oval = new RectF(top, top, bottom, bottom);
    canvas.drawArc(oval, 270, 360 * progress / max, opt, mPaint);
    mPaint.setColor(Color.BLACK);
    String text = progress + "%";
    float stringWidth = mPaint.measureText(text);
    float x = (getWidth() - stringWidth) / 2;
    //Log.e("Lixiaoliang", "小亮亮"+progress);
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setTextSize(70f);
    canvas.drawText(text, x, height / 2, mPaint);
}

private void initProperty(AttributeSet attrs) {

}

/**
 * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
 */
public synchronized void setProgress(int progress) {
    if (progress < 0) {
        progress = 0;
    } else if (progress > max) {
        progress = max;
    } else {
        this.progress = progress;
    }
}

public int getMax() {
    return max;
}

}

//MainActvity下
public class MainActivity extends Activity {
private AddView progressView;
private TextView mT1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mT1 = findViewById(R.id.text111);
progressView =findViewById(R.id.MyCircleProgress);
initAnmint();
new ProgressAnimation().execute();
}
private void initAnmint() {
//做缩放动画
ObjectAnimator scale = ObjectAnimator.ofFloat(mT1, “scaleX”, new float[]{0f,0.5f,1f,3f,1f});
//做透明动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(mT1, “alpha”, new float[]{0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
//做旋转动画
ObjectAnimator rotationY = ObjectAnimator.ofFloat(mT1, “rotationX”, new float[]{0f,90f, 160f, 270f, 360f});
AnimatorSet set = new AnimatorSet();
set.setDuration(5000);
set.playTogether(scale,alpha,rotationY);
set.start();
}

class ProgressAnimation extends AsyncTask<Void, Integer, Void> {
    int i1=0;
    @Override
    protected Void doInBackground(Void... params) {
        //进度值不断的变化
        for (int i = 0; i <= progressView.getMax(); i++) {
            //Log.e("Lixiaoliang", "wwwww"+i);
            try {
                i1=i;
                publishProgress(i);
                Thread.sleep(50);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (i1==100){
                            startActivity(new Intent(MainActivity.this,TwoActivity.class));
                            finish();
                        }
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        //更新进度值
        progressView.setProgress(values[0]);
        progressView.invalidate();

        super.onProgressUpdate(values);
    }
}

}

猜你喜欢

转载自blog.csdn.net/weixin_43117800/article/details/84348338