android 自定义ProgressBar 文字跟随进度效果

http://wx3.sinaimg.cn/large/958c5b69ly1fwns4dbebqg20920ijjsr.gif
mark

1 字体适配


    private void textSizeAdaptive() {
        //1.获取当前设备的屏幕大小
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        //2.计算与你开发时设定的屏幕大小的纵横比(这里假设你开发时定的屏幕大小是480*800)
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;
        float ratioWidth = (float) screenWidth / 1080;
        float ratioHeight = (float) screenHeight / 1920;

        ratio = Math.min(ratioWidth, ratioHeight);
        if (ratioWidth != ratioHeight) {
            if (ratio == ratioWidth) {
                offsetLeft = 0;
                offsetTop = Math.round((screenHeight - 1920 * ratio) / 2);
            } else {
                offsetLeft = Math.round((screenWidth - 1080 * ratio) / 2);
                offsetTop = 0;
            }
        }
        //3.根据上一步计算出来的最小纵横比来确定字体的大小(假定在1080*1920屏幕下字体大小设定为35)
        TEXT_SIZE = Math.round(textsize * ratio);
    }
  1. onDraw
@Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.save();
        mText = (getProgress() * 100 / getMax()) + "%";
        Rect rect = new Rect();
        mPaint.getTextBounds(leftText, 0, leftText.length(), rect);
        int y = (getHeight() / 2) - rect.centerY();

        //在进度条上画上自定义文本
        canvas.drawText(leftText, 5, y, mPaint);

        int width = rect.width();

        //进度
        float radio = getProgress() * 1.0f / getMax();
        float progressPosX = (int) (mRealWidth * radio);

        //起始点
        int beginX = 10 + width;
        //结束点
        float textWidth = mPaint.measureText(mText);
        float endX = mRealWidth - textWidth;
        if (beginX > progressPosX- textWidth) {
            canvas.drawText(mText, beginX, y, mPaint);
        } else if (progressPosX- textWidth > endX) {
            canvas.drawText(mText, endX, y, mPaint);
        } else {
            canvas.drawText(mText, progressPosX - textWidth, y, mPaint);
        }
        canvas.restore();
    }

下载地址 http://download.csdn.net/download/liudao7994/10130885

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值