您的位置:首页 > 移动开发 > Android开发

Android 自定义进度条

2017-08-15 19:01 253 查看
一、废话少说先上图



二、思路

主要是使用自定义view和自定义属性去实现刷新

三、代码

attr.xml

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

<declare-styleable name="CustomProgressBar">
<attr name="roundProgressColor" format="color" />
<attr name="roundColor" format="color" />
<attr name="roundWidth" format="dimension" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color" />
<attr name="max" format="integer" />
<attr name="textShow" format="boolean" />
<attr name="sleepTime" format="integer" />
<attr name="style">
<enum name="STROKE" value="0" />
<enum name="FILL" value="1" />
</attr>
</declare-styleable>
</resources>


CustomProgressBar.java

public class CustomProgressBar extends View {

private int roundProgressColor;
private int roundColor;
private float roundWidth;
private float textSize;
private int textColor;
private int max;
private boolean textShow;
private int style;
private Paint mPaint;
private final int STOKE = 0;
private final int FILL = 1;
private int progress;
private int sleepTime;//睡眠时间

public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
//获取自定义属性数组
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor, Color.RED);
roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.BLUE);
roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth, 50);
textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize, 25);
textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, Color.GREEN);
max = typedArray.getInteger(R.styleable.CustomProgressBar_max, 100);
textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow, false);
style = typedArray.getInteger(R.styleable.CustomProgressBar_style, 0);
sleepTime = typedArray.getInteger(R.styleable.CustomProgressBar_sleepTime, 100);
//释放TypedArray实例,从而使其可被其他模块复用
typedArray.recycle();

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画默认大圆环
int center = getWidth() / 2;//中心坐标点
float radius = center - roundWidth / 2;//半径

mPaint.setColor(roundColor);
mPaint.setStyle(Paint.Style.STROKE);//设置空心(描边)
//设置圆环宽度
mPaint.setStrokeWidth(roundWidth);
//设置抗锯齿
mPaint.setAntiAlias(true);
//绘制圆
canvas.drawCircle(center, center, radius, mPaint);

//绘制进度百分比
mPaint.setColor(textColor);
//圆环的宽度
mPaint.setStrokeWidth(0);
mPaint.setTextSize(textSize);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
int percent = (int) (progress / (float) max * 100);
if (textShow && percent != 0 && style == STOKE) {
//descent下基准线位置,ascent上基准线位置
canvas.drawText(percent + "%",
(getWidth() - mPaint.measureText(percent + "%")) / 2f,
getWidth() / 2f - (mPaint.descent() + mPaint.ascent()) / 2f,
mPaint);
}
//画圆弧
//矩形区域,定义圆弧的形状大小
RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
mPaint.setColor(roundProgressColor);
mPaint.setStrokeWidth(roundWidth);
switch (style) {
case STOKE:
mPaint.setStyle(Paint.Style.STROKE);
/**
* RectF 圆弧所在的椭圆对象,
* startAngle圆弧的起始角度
* sweepAngle圆弧的角度
* useCenter 是否显示半径连线,true表示显示圆弧与圆心的半径连线,false表示不显示。
* Paint 画笔
*/
canvas.drawArc(oval, 0, 360 * progress / max, false, mPaint);
break;
case FILL:
mPaint.setStyle(Paint.Style.FILL);
if (progress != 0)
canvas.drawArc(oval, 0, 360 * progress / max, true, mPaint);
break;
}
}

public synchronized int getMax() {
return max;
}

public synchronized void setMax() {
if (max < 0) {
throw new IllegalArgumentException("max不能小于0");
}
this.max = max;
}

public synchronized int getProgress() {
return progress;
}

public synchronized void setProgress(int progress) {
if (progress < 0) {
throw new IllegalArgumentException("progress不能小于0");
}
if (progress > max) {
progress = max;
}
if (progress <= max) {
this.progress = progress;
postInvalidate();
}
}

//开始绘制
public void startDraw() {
new Thread(new Runnable() {
@Override
public void run() {
while (progress <= 100) {
progress += 2;
setProgress(progress);
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}

public int getRoundColor() {
return roundColor;
}

public void setRoundColor(int roundColor) {
this.roundColor = roundColor;
}

public int getRoundProgressColor() {
return roundProgressColor;
}

public void setRoundProgressColor(int roundProgressColor) {
this.roundProgressColor = roundProgressColor;
}

public int getTextColor() {
return textColor;
}

public void setTextColor(int textColor) {
this.textColor = textColor;
}

public float getTextSize() {
return textSize;
}

public void setTextSize(int textSize) {
this.textSize = textSize;
}

public float getRoundWidth() {
return roundWidth;
}

public void setRoundWidth(int roundWidth) {
this.roundWidth = roundWidth;
}

public boolean isTextShow() {
return textShow;
}

public void setTextShow(boolean textShow) {
this.textShow = textShow;
}
}
main.xml

<com.test.view.CustomProgressBar
android:id="@+id/progressbar"
android:layout_width="100dp"
android:layout_height="100dp"
app:roundProgressColor="#ff00ff"
app:roundWidth="15dp"
app:textColor="#ff0000"
app:textShow="true"
app:textSize="20dp" />


然后获取自定义控件的对象,设置点击事件调用startDraw()就可以
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自定义进度条