使用TimerTask控制ProgressBar

有很多朋友使用Thread进行Progress的进度控制,这里我们使用Timer和TimerTask控制ProgressBar。  vkLG<Y  
 UI'eD)WR  
实现效果: 在进度75%之前,每秒钟有5%的进度,在75%之后每秒有1%的进度。

需要的知识: ( d.i np(  
1、ProgressBar *']RYu?X  
2、Thread usi p>y  
3、Timer和TimerTask 

Activity代码:

package cc.androidos.pb;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.ProgressBar;public class ProgressBarTest extends Activity{ /** Called when the activity is first created. */ private ProgressBar mProgress = null; /**Static for control the progress*/ public static int mProgressStatus = 0; /**Handler for post message into OS and change UI */ private Handler mHandler = new Handler(); @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); //set the View setContentView( R.layout.main ); mProgress = ( ProgressBar ) findViewById( R.id.ProgressBar01 ); //Progress Bar Click ! mProgress.setOnClickListener( new View.OnClickListener() { @Override public void onClick( View arg0 ) { ProgressBarTest.mProgressStatus = 0; // a new Timer Timer tt = new Timer(); //Schedule the TimerTash , delay is 0 ms, every 1000ms execute the action tt.schedule( new ReTimerTask(), 0, 1 * 1000 ); //Scout the progress scoutProgress.start(); } } ); } /** * Scout the progress */ Thread scoutProgress = new Thread( new Runnable() { public void run() { while ( mProgressStatus < 100 ) { // Update the progress bar mHandler.post( new Runnable() { public void run() { mProgress.setProgress( mProgressStatus ); } } ); } } } );}class ReTimerTask extends TimerTask{ @Override public void run() { //以下是监测进度到什么位置,如果在75范围内就对进度+5,如果在75----100之间就对进度+1,否则取消此任务 if( ProgressBarTest.mProgressStatus < 75 ) { ProgressBarTest.mProgressStatus += 5; System.err.println( "<75......." + ProgressBarTest.mProgressStatus ); } else if( ProgressBarTest.mProgressStatus < 100 && ProgressBarTest.mProgressStatus >= 75 ) { ProgressBarTest.mProgressStatus += 1; System.err.println( ">75 || <100......." + ProgressBarTest.mProgressStatus ); } else if( ProgressBarTest.mProgressStatus >= 100 ) { System.err .println( ">100......." + ProgressBarTest.mProgressStatus ); cancel(); } }} 

布局文件代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ProgressBar android:id="@+id/ProgressBar01" android:layout_width="100px" android:layout_height="20px" android:max="100" android:progress="0" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"//style设置是为了使得ProgressBar变成进度条而不是圆圈 > </ProgressBar></LinearLayout>

使用线程也可以处理进度,但也需要借助Handler投递改变UI的消息到操作系统中,从而改版ProgressBar。

你可能感兴趣的:(thread,android,timer,layout,Class,encoding)