Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android圆形进度条

Android自定义View实现圆形进度条

作者:360度旋转的猫

这篇文章主要为大家详细介绍了Android自定义View实现圆形进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义View实现圆形进度条的具体代码,供大家参考,具体内容如下

原理非常简单,在自定义View的基础上使用Canvas的drawCircle画两个圆,一个圆代表总体,一个圆代表现有进度,在主线程中使用set方法在Handler中传递进度,进度和总体都可以添加set、get方法进行自定义

//主线程代码如下

public class MainActivity extends Activity {
private Button mBtn_start;
private MyProgresscircle myProgresscircle;
private int progress;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
progress++;
if(progress<=100){//形成循环
myProgresscircle.setCurrentProgress(progress);//传递进度
handler.sendEmptyMessageDelayed(0,200);//模拟下载延迟
                    }
break;
            }
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
myProgresscircle= (MyProgresscircle) findViewById(R.id.myprogress);
mBtn_start= (Button) findViewById(R.id.button_start);
mBtn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(0);//启动下载发送一个空消息开始
            }
        });
    }
}

//自定义View代码:

public class MyProgresscircle extends View {
private int width;
private int height;
private Paint mPaintText;
private Paint mPaintCircle;
private Paint mPaintCircleCurrent;
private int CurrentProgress;
private int MaxProgress=100;

public void setCurrentProgress(int currentProgress) {

CurrentProgress = currentProgress;
        invalidate();
    }

public int getMaxProgress() {
return MaxProgress;
    }

public void setMaxProgress(int maxProgress) {
MaxProgress = maxProgress;
    }

public MyProgresscircle(Context context) {
super(context);
    }

public MyProgresscircle(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintText=new Paint();//字体进度画笔
mPaintText.setTextSize(80);
mPaintText.setColor(Color.WHITE);
mPaintText.setStrokeWidth(10);
mPaintText.setTextAlign(Paint.Align.CENTER);//偏移

mPaintCircle=new Paint();//外圆画笔
mPaintCircle.setColor(Color.GRAY);
mPaintCircle.setAntiAlias(true);
mPaintCircle.setStrokeWidth(10);

mPaintCircleCurrent=new Paint();//进度圆画笔
mPaintCircleCurrent.setColor(Color.YELLOW);
mPaintCircleCurrent.setAntiAlias(true);
mPaintCircleCurrent.setStrokeWidth(10);
    }

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//这个复写方法用来得到宽和高
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
        canvas.drawCircle(width / 2, height / 2, 200, mPaintCircle);
        canvas.drawCircle(width/2,height/2,CurrentProgress/100f*200,mPaintCircleCurrent);
        canvas.drawText(CurrentProgress*100f/MaxProgress+"%",width/2,height/2,mPaintText);//把进度转化为百分比
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文