Android TextProgressBar

来源:互联网 发布:软件开发基础培训 编辑:程序博客网 时间:2024/06/05 17:36
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();    }}@Overrideprotected 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/

原创粉丝点击