自定义ProgressBar

本文介绍了一种自定义ProgressBar的方法,通过创建ProgressViewBar类并继承View,实现了一个可自定义颜色、最大值和当前值的进度条组件。文章详细展示了如何在Android中测量和绘制这个自定义视图,包括如何设置其宽度、高度以及如何使用Canvas和Paint来绘制背景和进度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义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();
    }
}

 

原生ProgresssBar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值