自定義進(jìn)度條
一、先上效果圖
二纫雁、思路
1澳盐、繪制底色(灰色)
2、繪制進(jìn)度(藍(lán)色)
3讼溺、繪制最大的進(jìn)度(純藍(lán)色)
三楣号、實(shí)現(xiàn)
1)繪制底色
RectF rf = new RectF(0, 0, mWidth, mHeight);
/*繪制圓角矩形,背景色為畫筆顏色*/
mPaint.setColor(Color.rgb(220, 220, 220));
canvas.drawRoundRect(rf, round, round, mPaint);
/*設(shè)置progress內(nèi)部是灰色*/
mPaint.setColor(Color.rgb(242, 241, 246));
RectF rectBlackBg = new RectF(3, 3, mWidth - 3, mHeight - 3);
canvas.drawRoundRect(rectBlackBg, round, round, mPaint);
2)繪制進(jìn)度
isMax 判斷是否為最大的進(jìn)度
//設(shè)置進(jìn)度條進(jìn)度及顏色
float section = currentCount / maxCount;
RectF rectProgressBg = new RectF(0, 0, mWidth * section, mHeight);
if (section != 0.0f) {
if (isMax){
mPaint.setColor(Color.rgb(75, 199, 247));
canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
}else {
RectF s1 = new RectF(0, 0, mWidth * section, mHeight);
mPaint.setColor(Color.rgb(83, 202, 247));
canvas.drawRoundRect(s1, round, round, mPaint);
RectF s2 = new RectF(3, 3, mWidth * section-3, mHeight-3);
mPaint.setColor(Color.rgb(210, 232, 245));
canvas.drawRoundRect(s2, round, round, mPaint);
}
} else {
mPaint.setColor(Color.TRANSPARENT);
canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
}
3)ProgressView完整代碼
public class ProgressView extends View {
/**
* 進(jìn)度條最大值
*/
private float maxCount;
/**
* 進(jìn)度條當(dāng)前值
*/
private float currentCount;
/**
* 畫筆
*/
private Paint mPaint;
/**
* 設(shè)置為最大項(xiàng)
**/
private boolean isMax;
private int mWidth, mHeight;
public ProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ProgressView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/***
* 設(shè)置最大的進(jìn)度值
* @param maxCount
*/
public void setMaxCount(float maxCount) {
this.maxCount = maxCount;
}
/***
* 設(shè)置為最大項(xiàng)
*/
public void setIsMax(boolean b) {
this.isMax = b;
}
/***
* 設(shè)置當(dāng)前的進(jìn)度值
* @param currentCount
*/
public void setCurrentCount(float currentCount) {
this.currentCount = currentCount > maxCount ? maxCount : currentCount;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mPaint = new Paint();
//設(shè)置抗鋸齒效果
mPaint.setAntiAlias(true);
//設(shè)置畫筆顏色
mPaint.setColor(Color.BLACK);
int round = mHeight / 2;
RectF rf = new RectF(0, 0, mWidth, mHeight);
/*繪制圓角矩形肾胯,背景色為畫筆顏色*/
mPaint.setColor(Color.rgb(220, 220, 220));
canvas.drawRoundRect(rf, round, round, mPaint);
/*設(shè)置progress內(nèi)部是灰色*/
mPaint.setColor(Color.rgb(242, 241, 246));
RectF rectBlackBg = new RectF(3, 3, mWidth - 3, mHeight - 3);
canvas.drawRoundRect(rectBlackBg, round, round, mPaint);
//設(shè)置進(jìn)度條進(jìn)度及顏色
float section = currentCount / maxCount;
RectF rectProgressBg = new RectF(0, 0, mWidth * section, mHeight);
if (section != 0.0f) {
if (isMax){
mPaint.setColor(Color.rgb(75, 199, 247));
canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
}else {
RectF s1 = new RectF(0, 0, mWidth * section, mHeight);
mPaint.setColor(Color.rgb(83, 202, 247));
canvas.drawRoundRect(s1, round, round, mPaint);
RectF s2 = new RectF(3, 3, mWidth * section-3, mHeight-3);
mPaint.setColor(Color.rgb(210, 232, 245));
canvas.drawRoundRect(s2, round, round, mPaint);
}
} else {
mPaint.setColor(Color.TRANSPARENT);
canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
}
}
private int dipToPx(int dip) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
//MeasureSpec.EXACTLY竖席,精確尺寸
if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
mWidth = widthSpecSize;
} else {
mWidth = 0;
}
//MeasureSpec.AT_MOST,最大尺寸敬肚,只要不超過父控件允許的最大尺寸即可毕荐,MeasureSpec.UNSPECIFIED未指定尺寸
if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
mHeight = dipToPx(20);
} else {
mHeight = heightSpecSize;
}
//設(shè)置控件實(shí)際大小
setMeasuredDimension(mWidth, mHeight);
}
}
如有意見和建議,及時(shí)溝通艳馒。