效果图是这样的:
接下来为大家粘贴代码:
首先是自定义的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() / 1) - rect.centerX() - 18;
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);
}
private void setText() {
setText(this.getProgress());
}
// 设置文字内容
private void setText(int progress) {
int i = (progress * 100) / this.getMax();
this.text = String.valueOf(i) + "%";
}
}
再者就是 actiivty里面的代码:
public class ProgressTest extends Activity {
private Button btn_go = null;
private MyProgress myProgress = null;
private Handler mHandler;
public TextView baifen;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
setParam();
addListener();
mHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
myProgress.setProgress(msg.what);
return false;
}
});
}
private void findView() {
btn_go = (Button) findViewById(R.id.btn_go);
myProgress = (MyProgress) findViewById(R.id.pgsBar);
// baifen = (TextView) findViewById(R.id.baifen);
}
private void setParam() {
btn_go.setText("开始");
}
private void addListener() {
btn_go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i <= 50; i++) {
mHandler.sendEmptyMessage(i * 2);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
});
}
}
还有就是需要在res文件夹下创建一个drawable文件夹;*****.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<corners android:radius="3dip" />
<gradient
android:angle="270"
android:centerY="0.75"
android:endColor="#999999"
android:startColor="#999999" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="0dip" />
<gradient
android:angle="270"
android:centerY="0.75"
android:endColor="#23b7a1"
android:startColor="#23b7a1" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerY="0.75"
android:endColor="#23b7a1"
android:startColor="#23b7a1" />
</shape>
</clip>
</item>
</layer-list>
values文件夹下 stytles.xml 里面添加代码:
<!-- 自定义进度条 -->
<style name="ProgressBar_Mini" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:maxHeight">50dip</item>
<item name="android:minHeight">12dip</item>
<item name="android:indeterminateOnly">false</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/progressbar_mini</item>
</style>
基本所有代码我都粘贴到 这里了,里面会用到一些颜色值,这个你们可以自己定义。
如果有用到的,不清楚怎么写的,可以加我QQ:2268214831 或者微信:hanxinghui0817 私聊我哦
本人经验不足,如有什么错误还请大神多多指点,在这里谢谢啦!希望本博文哭帮助到一些人!
来源:oschina
链接:https://my.oschina.net/u/2277819/blog/500766