android|在ProgressBar上加文字----显示百分比的进度条



import android.content.Context;
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 MyProgress extends ProgressBar{
String text;
Paint mPaint;

public MyProgress(Context context) {
super(context);
// TODO Auto-generated constructor stub
System.out.println("1");
initText();
}

public MyProgress(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
System.out.println("2");
initText();
}


public MyProgress(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
System.out.println("3");
initText();
}

@Override
public synchronized void setProgress(int progress) {
// TODO Auto-generated method stub
setText(progress);
super.setProgress(progress);

}

@Override
protected synchronized void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//this.setText();
Rect rect = new Rect();
this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
canvas.drawText(this.text, x, y, this.mPaint);
}

//初始化,画笔
private void initText(){
this.mPaint = new Paint();
this.mPaint.setColor(Color.WHITE); this.mPaint.setTextSize(18);
//去掉锯齿
this.mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

}

private void setText(){
setText(this.getProgress());
}

//设置文字内容
private void setText(int progress){
int i = (progress * 100)/this.getMax();
this.text = String.valueOf(i) + "%";
}


}



----------------------------------------------------------
package com.xxxxxxapplaction.ui;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;


public class TestOntimeStartTestActivity extends Activity{





private ProgressBar progressBar;
private Button startButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_start_test);

progressBar = (ProgressBar) findViewById(R.id.progress_horizontal);
progressBar.setVisibility(View.VISIBLE);
// 添加updataThread到消息队列
handler.post(updateThread);


}


// 使用匿名内部类来复写Handler当中的handlerMessage()方法
// 这个handler是需要反复执行的,直到消息队列中没有Message为止。
// 执行过程中把进度条就刷新了。
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
System.out.println("----------start handle");
progressBar.setProgress(msg.arg1);
handler.post(updateThread); // 将要执行的线程放入到队列当中
}
};

// 线程类,该类使用匿名内部类的方式进行声明
Runnable updateThread = new Runnable() {
int i = 0;

public void run() {
// TODO Auto-generated method stub
System.out.println("----------Begin Thread");
i += 5;
// 得到一个消息对象,Message类是android系统提供的
Message msg = handler.obtainMessage();
// 将Message对象的arg1参数的值设置为i
msg.arg1 = i; // 用arg1、arg2这两个成员变量传递消息,优点是系统性能消耗较少
try {
Thread.sleep(1000); // 让当前线程休眠1000毫秒
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// 将Message对象加入到消息队列当中,等下次looper调用消息队列时,使用这个message显示进度条
// 或者说,把增加了10%的进度信息Message放到updateBarHandler的队列中。
if (i < 101){
handler.sendMessage(msg);
}
// 如果i的值等于100
if (i == 100) {
// 将线程对象从队列中移除
handler.removeCallbacks(updateThread);
}
}
};
}




android:id="@+id/progress_horizontal"
android:layout_below="@id/proccesstext"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:max="100"
android:progress="0"
android:layout_centerHorizontal="true"
android:background="@drawable/progress_bg"
android:secondaryProgress="0" />


    推荐阅读