简单自定义水平的ProgressBar带文字

首先来看看实图


这里实现原理就是继承ProgressBar重写onDraw方法,在ondraw里面用画笔绘制text


首先初始化paint


首先获取手机屏幕的宽高,根据手机分辨率来定标准字体大小,这里采用的720x1080(比较通用)



获取进度,设置文字内容





最后就是onDraw绘制了

@Override
protected synchronized void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Rect rect = new Rect();
    this.mPaint.getTextBounds(text, 0, text.length(), rect);
    this.mPaint.getTextBounds(text1, 0, text1.length(), rect);
    
    int progress = getProgress();
    int width = getWidth();
    
/**以免getmax为0
    if (getMax() != 0)
    {
        int start = width / getMax() * progress;
        int end = width / getMax() * (getMax() - progress);
        int x_two = ((end / 2) + start) - rect.centerX();
        int x_one = (start / 2) - rect.centerX();
        int y = (getHeight() / 2) - rect.centerY();
        if (getProgress() == 0)
        {
            canvas.drawText(this.text1, x_two, y, this.mPaint);
        }
        else
        {
            canvas.drawText(this.text, x_one, y, this.mPaint);
            canvas.drawText(this.text1, x_two, y, this.mPaint);
        }
    }
}
到这里就完成了。
代码下载地址http://download.csdn.net/download/lmy545x/10038852


   











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单自定义水平进度条的代码实现,根据后台返回的数据显示进度: 1. 首先,在res/layout文件夹下新建一个xml文件,例如progress_bar_horizontal.xml,添加如下代码: ```xml <com.example.myapplication.HorizontalProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" app:max="100" app:progress="0" app:progress_color="@color/colorAccent" app:secondary_progress_color="@color/colorPrimary" app:background_color="@color/colorGray" app:text_color="@color/colorBlack" app:text_size="14sp" app:text_visible="true" /> ``` 其中,自定义的HorizontalProgressBar组件继承自ProgressBar,所以在xml中使用时需要写全类名。 2. 在res/values文件夹下新建一个attrs.xml文件,添加自定义属性: ```xml <resources> <declare-styleable name="HorizontalProgressBar"> <attr name="progress_color" format="color" /> <attr name="secondary_progress_color" format="color" /> <attr name="background_color" format="color" /> <attr name="text_color" format="color" /> <attr name="text_size" format="dimension" /> <attr name="text_visible" format="boolean" /> <attr name="max" format="integer" /> <attr name="progress" format="integer" /> </declare-styleable> </resources> ``` 3. 新建一个HorizontalProgressBar.java文件,添加如下代码: ```java public class HorizontalProgressBar extends ProgressBar { private int mTextColor; private float mTextSize; private boolean mTextVisible; private int mProgressColor; private int mSecondaryProgressColor; private int mBackgroundColor; private Paint mPaint; private Rect mRect; public HorizontalProgressBar(Context context) { super(context); init(null); } public HorizontalProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制背景 mPaint.setColor(mBackgroundColor); canvas.drawRect(mRect, mPaint); // 绘制进度 float progressRatio = (float)getProgress() / (float)getMax(); int progressWidth = (int)(progressRatio * getWidth()); mPaint.setColor(mProgressColor); canvas.drawRect(0, 0, progressWidth, getHeight(), mPaint); // 绘制次进度 float secondaryProgressRatio = (float)getSecondaryProgress() / (float)getMax(); int secondaryProgressWidth = (int)(secondaryProgressRatio * getWidth()); mPaint.setColor(mSecondaryProgressColor); canvas.drawRect(0, 0, secondaryProgressWidth, getHeight(), mPaint); // 绘制文字 if (mTextVisible) { mPaint.setColor(mTextColor); mPaint.setTextSize(mTextSize); String progressText = getProgress() + "%"; float textWidth = mPaint.measureText(progressText); float textX = (getWidth() - textWidth) / 2; float textY = (getHeight() + mTextSize) / 2 - mPaint.descent(); canvas.drawText(progressText, textX, textY, mPaint); } } private void init(AttributeSet attrs) { // 初始化属性 if (attrs != null) { TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar); mTextColor = ta.getColor(R.styleable.HorizontalProgressBar_text_color, Color.BLACK); mTextSize = ta.getDimension(R.styleable.HorizontalProgressBar_text_size, 14); mTextVisible = ta.getBoolean(R.styleable.HorizontalProgressBar_text_visible, true); mProgressColor = ta.getColor(R.styleable.HorizontalProgressBar_progress_color, Color.BLUE); mSecondaryProgressColor = ta.getColor(R.styleable.HorizontalProgressBar_secondary_progress_color, Color.GRAY); mBackgroundColor = ta.getColor(R.styleable.HorizontalProgressBar_background_color, Color.TRANSPARENT); ta.recycle(); } else { mTextColor = Color.BLACK; mTextSize = 14; mTextVisible = true; mProgressColor = Color.BLUE; mSecondaryProgressColor = Color.GRAY; mBackgroundColor = Color.TRANSPARENT; } // 初始化画笔 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mRect = new Rect(0, 0, getWidth(), getHeight()); } } ``` 4. 在Activity中获取ProgressBar组件,并根据后台返回的数据设置进度,例如: ```java HorizontalProgressBar progressBar = findViewById(R.id.progress_bar); int progress = (int)(data/current * 100); // data为当前值,current为总值 progressBar.setProgress(progress); ``` 这样就完成了自定义水平进度条的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值