TOP5_3:定制简单的进度条
结构:
Activity:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 4 android:layout_width="match_parent" android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.top5.MainActivity"> 10 11 <com.example.top5_3.MyProgressBar 12 android:id="@+id/pb" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:max="100" 16 style="@android:style/Widget.ProgressBar.Horizontal"/> 17 </RelativeLayout>
JAVA:
MainActivity:
1 package com.example.top5_3; 2 3 4 5 6 import android.app.Activity; 7 import android.os.Handler; 8 import android.os.Message; 9 10 import android.os.Bundle; 11 import android.view.Menu; 12 import android.widget.ProgressBar; 13 14 public class MainActivity extends Activity { 15 private MyProgressBar pb; 16 17 Handler hd = new Handler() { 18 public void handleMessage(Message msg) { 19 pb.setProgress(msg.what); 20 } 21 }; 22 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 pb = (MyProgressBar) findViewById(R.id.pb); 27 new Thread(new Runnable() { 28 @Override 29 public void run() { 30 for (int i = 1; i <= 100; i+=1) { 31 Message mg = new Message(); 32 mg.what = i; 33 hd.sendMessage(mg); 34 try { 35 Thread.sleep(1000); 36 } catch (InterruptedException e) { 37 e.printStackTrace(); 38 } 39 } 40 } 41 }).start(); 42 } 43 44 public boolean onCreateOptionsMenu(Menu menu) 45 { 46 getMenuInflater().inflate(R.menu.main, menu); 47 return true; 48 } 49 50 }
MyProgressBar:
1 package com.example.top5_3; 2 3 import java.text.MessageFormat; 4 5 import android.content.Context; 6 import android.graphics.Canvas; 7 import android.graphics.Color; 8 import android.graphics.Paint; 9 import android.graphics.Paint.Align; 10 import android.graphics.Paint.FontMetrics; 11 import android.util.AttributeSet; 12 import android.widget.ProgressBar; 13 14 public class MyProgressBar extends ProgressBar { 15 private Paint paint=new Paint(); 16 public MyProgressBar(Context context) { 17 super(context); 18 // TODO Auto-generated constructor stub 19 } 20 public MyProgressBar(Context cont,AttributeSet attrs) { 21 // TODO Auto-generated constructor stub 22 super(cont,attrs); 23 } 24 protected synchronized void onDraw(Canvas cv){ 25 super.onDraw(cv); 26 //进度天百分比 27 int reta=(int)((double)getProgress()/this.getMax()*100); 28 //要绘制的文字 29 String tip=MessageFormat.format("{0}%",reta); 30 //设置文字对齐方式 31 paint.setTextAlign(Align.CENTER); 32 //设置文字颜色 33 paint.setColor(Color.BLUE); 34 //设置字体大小 35 paint.setTextSize(24); 36 //Canvas绘制文本时,使用 fontMetrics对象,计算坐标位置 37 FontMetrics fmt=paint.getFontMetrics(); 38 int h=(int)((fmt.bottom-fmt.top)/2-fmt.bottom); 39 //进行绘制 40 cv.drawText(tip, this.getWidth()/2, this.getHeight()/2+h, paint); 41 42 } 43 }
运行效果: