Android进度条带背景图和百分比

效果图:

百分比可以在布局文件中通过app:cpbShowPercent="true"配置显示或隐藏

代码简单易读,可自行定制修改

附上代码:

public class CustomProgressBar extends ProgressBar {
    private static final String TAG = "CustomProgressBar";
    private Paint mPaint;
    private Bitmap bitmap;
    private int bitmapWidth;
    private int cpbIcon;
    private int cpbPercentColor = 0xffffffff;
    private int cpbPercentSize = sp2px(14);
    private boolean cpbShowPercent = false;

    public CustomProgressBar(Context context) {
        this(context, null);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.customProgressBar);
        cpbPercentColor = typedArray.getColor(R.styleable.customProgressBar_cpbPercentColor, cpbPercentColor);
        cpbPercentSize = typedArray.getDimensionPixelSize(R.styleable.customProgressBar_cpbPercentSize, cpbPercentSize);
        cpbIcon = typedArray.getResourceId(R.styleable.customProgressBar_cpbIcon, cpbIcon);
        cpbShowPercent = typedArray.getBoolean(R.styleable.customProgressBar_cpbShowPercent, cpbShowPercent);
        typedArray.recycle();
        init();
    }

    private void init() {


        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(cpbPercentColor);
        mPaint.setTextSize(cpbPercentSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        bitmap = BitmapFactory.decodeResource(getResources(), cpbIcon);
        bitmapWidth = bitmap.getWidth();

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, bitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int currentProgress = this.getProgress();
        int mWidth = this.getWidth();
        int offset = (int) (currentProgress * 1.0 / getMax() * mWidth);
        int moveDis = 0;
        if (offset >= (bitmapWidth / 2) && offset <= (mWidth - bitmapWidth / 2)) {
            moveDis = offset - bitmapWidth / 2;
        } else if (offset < (bitmapWidth / 2)) {
            moveDis = 0;
        } else if (offset > (mWidth - bitmapWidth / 2)) {
            moveDis = mWidth - bitmapWidth;
        }
        canvas.drawBitmap(bitmap, moveDis, 0, mPaint);

        if (cpbShowPercent) {
            String text = currentProgress + "%";
            //获取文字的高度
            Rect bounds = new Rect();
            mPaint.getTextBounds(text, 0, text.length(), bounds);
            canvas.drawText(text, bitmap.getWidth() / 2 + moveDis, bitmap.getHeight() / 2 + bounds.height() / 2, mPaint);
        }
    }

    private int sp2px(float v) {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, v, displayMetrics);
    }
}

相关属性:

 <declare-styleable name="customProgressBar">
        <attr name="cpbIcon" format="reference"/>
        <attr name="cpbPercentColor" format="color"/>
        <attr name="cpbPercentSize" format="dimension"/>
        <attr name="cpbShowPercent" format="boolean"/>
    </declare-styleable>

使用:按照ProgressBar的使用方法使用即可

<com.wobuzhidao.iflydemo.CustomProgressBar
        android:id="@+id/pb"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="0"
        app:cpbIcon="@drawable/fly"
        app:cpbPercentSize="14sp"
        app:cpbPercentColor="@color/colorAccent"
        app:cpbShowPercent="true"/>

更多知识可以关注公众号获取

欢迎大家关注我的公众号,微信搜索、扫一扫或者长按识别二维码均可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩~晓强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值