Android 描边颜色渐变进度条

public class ColorGradientProgressBar extends View {

 

/**

* 渐变颜色组

*/

private int[] GRADIENT_COLORS = {Color.parseColor("#85D0E8"), Color.parseColor("#138CCF")};

/**

* 最大进度

*/

private float max = 100;

/**

* 当前进度

*/

private float progress;

/**

* 画笔

*/

private Paint mPaint;

/**

* 外描边的宽度

*/

private float BORDER_STROCK;

/**

* 进度条进度矩形与控件边界的距离,≥BORDER_STROCK

*/

private float PROGRESS_STROCK;

//进度条的宽高

private int mWidth, mHeight;

/**

* 画进度条的矩形

*/

private RectF mRectF;

 

public ColorGradientProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initView();

}

 

public ColorGradientProgressBar(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

 

public ColorGradientProgressBar(Context context) {

this(context, null);

}

 

private void initView() {

mPaint = new Paint();

mPaint.setAntiAlias(true);

mRectF = new RectF();

BORDER_STROCK = getResources().getDimension(R.dimen.x3);

PROGRESS_STROCK = getResources().getDimension(R.dimen.x5);

}

 

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

 

int round = mHeight / 2;//弧度为高度的一半

mRectF.set(0, 0, mWidth, mHeight);//第一层矩形(描边层)

mPaint.setColor(Color.parseColor("#A3D8E8"));//第一层矩形颜色(进度条描边的颜色)

canvas.drawRoundRect(mRectF, round, round, mPaint);//画第一层圆角矩形

mPaint.setColor(Color.parseColor("#474C4E"));//第二层矩形颜色(背景层颜色)

mRectF.set(BORDER_STROCK, BORDER_STROCK, mWidth - BORDER_STROCK, mHeight - BORDER_STROCK);//第二层矩形(背景层)

canvas.drawRoundRect(mRectF, round, round, mPaint);//画背景层圆角矩形(盖在描边层之上)

if (progress == 0)//进度为 0不画进度

return;

float section = progress / max;

//第三层矩形(进度层)

mRectF.set(PROGRESS_STROCK, PROGRESS_STROCK, (mWidth - PROGRESS_STROCK) * section, mHeight - PROGRESS_STROCK);

//创建线性颜色渐变器

LinearGradient shader = new LinearGradient(PROGRESS_STROCK, PROGRESS_STROCK,

(mWidth - PROGRESS_STROCK) * section, mHeight - PROGRESS_STROCK, GRADIENT_COLORS, null, Shader.TileMode.MIRROR);

mPaint.setShader(shader);//第三层矩形颜色(进度渐变色)

canvas.drawRoundRect(mRectF, round, round, mPaint);//画第三层(进度层)圆角矩形(盖在背景层之上)

mPaint.setShader(null);//清除之前传递的shader

}

 

/***

* 设置最大进度

*

* @param maxCount

*/

public void setMax(float maxCount) {

this.max = maxCount;

}

 

/***

* 设置当前进度

*

* @param currentCount

*/

public void setProgress(float currentCount) {

this.progress = currentCount > max ? max : currentCount;

invalidate();

}

 

public float getMax() {

return max;

}

 

 

/**

* 测量得到进度条的宽高

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {

mWidth = widthSpecSize;

} else {

mWidth = 0;

}

if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {

mHeight = (int) getResources().getDimension(R.dimen.x20);

} else {

mHeight = heightSpecSize;

}

setMeasuredDimension(mWidth, mHeight);

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值