Android TextProgressBar

TextProgressBar class should look like this:
package com.wvr.widget;

import com.wvr.example.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class TextProgressBar extends ProgressBar {

private String text = "";
private int textColor = Color.BLACK;
private float textSize = 15;

public TextProgressBar(Context context) {
    super(context);
}

public TextProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    setAttrs(attrs);
}

public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setAttrs(attrs);
}

private void setAttrs(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextProgressBar, 0, 0);
        setText(a.getString(R.styleable.TextProgressBar_text));
        setTextColor(a.getColor(R.styleable.TextProgressBar_textColor, Color.BLACK));
        setTextSize(a.getDimension(R.styleable.TextProgressBar_textSize, 15));
        a.recycle();
    }
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(textColor);
    textPaint.setTextSize(textSize);
    Rect bounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    int x = getWidth() / 2 - bounds.centerX();
    int y = getHeight() / 2 - bounds.centerY();
    canvas.drawText(text, x, y, textPaint);
}

public String getText() {
    return text;
}

public synchronized void setText(String text) {
    if (text != null) {
        this.text = text;
    } else {
        this.text = "";
    }
    postInvalidate();
}

public int getTextColor() {
    return textColor;
}

public synchronized void setTextColor(int textColor) {
    this.textColor = textColor;
    postInvalidate();
}

public float getTextSize() {
    return textSize;
}

public synchronized void setTextSize(float textSize) {
    this.textSize = textSize;
    postInvalidate();
}
}


And,The last thing I’d like to cover here is branding. At the screenshot above you can see default ProgressBar look and feel. Lets do couple improvements to make it looks nice. To do this we will create another file /res/drawable/progressbar.xml with the following structure:


<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
           android:angle="270"
           android:centerColor="#e7e5e5"
           android:centerY="0.75"
           android:endColor="#cfcdcd"
           android:startColor="#eceaea" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
               android:angle="270"
               android:centerColor="#80ffb600"
               android:centerY="0.75"
               android:endColor="#a0ffcb00"
               android:startColor="#80ffd300" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
               android:angle="270"
               android:centerColor="#53a2b9"
               android:centerY="0.75"
               android:endColor="#53a2b9"
               android:startColor="#53a2b9" />
        </shape>
    </clip>
</item>
</layer-list>


Add to attr.xml

<resources>
<declare-styleable name="TextProgressBar">
    <attr name="text" format="string" />
    <attr name="textColor" format="color" />
    <attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>



s you see we can set corner radius. Also, using gradient you can set background color to both parts. Let’s now apply this style using progressDrawable property. Remember to add following line to specify the component path in the layout xml:

xmlns:components="http://schemas.android.com/apk/res/com.xxx.xxx"


Then, add the TextProgressBar into the XML as following:

<com.wvr.widget.TextProgressBar
   android:id="@+id/progressBarWithText"
   style="@android:style/Widget.ProgressBar.Horizontal"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:max="100"
   android:maxHeight="30dp"
   android:minHeight="30dp"
   android:progress="70"
   android:progressDrawable="@drawable/progressbar"
   components:textSize="18dp" 
   components:textColor="@android:color/black" 
   components:text="Loading 70%" />


转自:http://weavora.com/blog/2012/02/23/android-progressbar-with-text/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值