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"/>

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

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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 AndroidProgressBar 组件来实现图片进度条。具体实现步骤如下: 1. 在你的布局文件中添加一个 ProgressBar 组件,并设置其 style 为 `@android:style/Widget.ProgressBar.Horizontal`,这样进度条就会呈现水平方向。 ``` <ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:indeterminate="false" android:max="100" android:progress="0" android:progressDrawable="@drawable/custom_progress_bar" android:secondaryProgress="0" android:style="@android:style/Widget.ProgressBar.Horizontal" /> ``` 2. 创建一个自定义的 Drawable 类,用于设置进度条的样式,包括进度条的背景和进度条的前景图片。你可以根据自己的需要进行修改。 ``` public class CustomProgressBarDrawable extends LayerDrawable { public CustomProgressBarDrawable(Drawable[] layers) { super(layers); } @Override protected boolean onLevelChange(int level) { Drawable progress = findDrawableByLayerId(android.R.id.progress); if (progress != null) { progress.setLevel(level); } return true; } } ``` 3. 在代码中获取 ProgressBar 组件,并设置进度条的样式为自定义的 Drawable。 ``` ProgressBar progressBar = findViewById(R.id.progressBar); Drawable progressDrawable = getResources().getDrawable(R.drawable.custom_progress_bar); CustomProgressBarDrawable customProgressBarDrawable = new CustomProgressBarDrawable(new Drawable[] {progressDrawable}); progressBar.setProgressDrawable(customProgressBarDrawable); ``` 这样,你就可以实现一个图片进度条了。需要注意的是,你需要根据自己的需求进行样式的修改,比如进度条的背景色、前景图片等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

韩~晓强

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

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

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

打赏作者

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

抵扣说明:

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

余额充值