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"
<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/
- Android TextProgressBar
- Android控件TextProgressBar进度条上显文字
- Android控件TextProgressBar进度条上显文字
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- android
- 老码农教你学英语:补充一些英语学习素材
- 最好自己手动敲下代码,对理解有很大帮助。
- ora-00054:resource busy and acquire with nowait specified解决方法
- JavaEE6-异步Servlet Sample
- More Effective C++:理解new和delete
- Android TextProgressBar
- Berkeley DB 的简介
- POJ1000
- MySQL分区表
- Nginx配置文件详细说明
- 用PHP对一张图片进行九宫格位置的随机水印添加 (个人学习笔记)
- Overview of HEVC之5 帧间预测
- spring mvc 中文乱码 post与get的方法解决
- 巧用“%”(不断更新中)