自定义进度条就是在画圆然后设一个监听,设一个耗时操作返回给进度条。
首先看一看布局文件
然后在进行一个绘制。
/**
* date:2018/11/21
* author:李晓亮:Shinelon
* function:
*/
public class MyCircleProgress 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 MyCircleProgress(Context context) {
this(context,null);
}
public MyCircleProgress(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyCircleProgress(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; }
}
然后在MainActivity里面进行耗时操作和更新进度条。
private MyCircleProgress 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 {
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);
}
}
}
这样就差不多了,仅供参考,至于实现具体的效果,看你自己的了。