自定义progressBar
ProgressViewBar progress_view_bar = carRateLy.findViewById(R.id.progress_view_bar);
progress_view_bar.setMainColor(R.color.colorPrimary);
progress_view_bar.setMax(Float.valueOf(car_rate.get(i).getRate2()));
progress_view_bar.setProgress(Float.valueOf(car_rate.get(i).getRate1()));
<com.bookMarker.views.ProgressViewBar
android:id="@+id/progress_view_bar"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:layout_weight="1"
android:layout_marginRight="8dp" />
public class ProgressViewBar extends View {
private float maxCount = 100;
private float currentCount = 50;
private Paint mPaint;
private int mWidth,mHeight,mainColor = R.color.colorPrimary;
public ProgressViewBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ProgressViewBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ProgressViewBar(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
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 = DubToolUtils.dip2px(getContext(),20);
} else {
mHeight = heightSpecSize;
}
//设置控件实际大小
setMeasuredDimension(mWidth, mHeight);
mWidth = mWidth-getPaddingLeft()-getPaddingRight();
mHeight = mHeight-getPaddingTop()-getPaddingBottom();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(getContext().getResources().getColor(mainColor));
int round = mHeight/2,left = getPaddingLeft(),top = getPaddingTop();
RectF rf = new RectF(left, top, mWidth, mHeight);
canvas.drawRoundRect(rf, round, round, mPaint);//绘制圆角矩形背景
mPaint.setColor(Color.WHITE);
RectF rectBlackBg = new RectF(left + 2, top + 2, mWidth-2, mHeight-2);
canvas.drawRoundRect(rectBlackBg, round, round, mPaint);//绘制进度条内部背景
float section = currentCount/maxCount;
RectF rectProgressBg = new RectF(left + 6, top + 6, (mWidth-6)*section, mHeight-6);
if(section!=0.0f){
mPaint.setColor(getContext().getResources().getColor(mainColor));
}else{
mPaint.setColor(Color.TRANSPARENT);
}
canvas.drawRoundRect(rectProgressBg, round, round, mPaint);//绘制进度条进度
}
public void setMainColor(int mainColor) {
this.mainColor = mainColor;
invalidate();
}
public void setMax(float maxCount) {
this.maxCount = maxCount;
}
public void setProgress(float currentCount) {
this.currentCount = currentCount > maxCount ? maxCount : currentCount;
invalidate();
}
}