ProgressBar進度條, 分爲旋轉進度條和水平進度條,進度條的樣式根據需要自定義,之前一直不明白進度條如何在實際項目中使用,網上演示進度條的案例大多都是通過Button點 擊增加、減少進度值,使用方法incrementProgressBy(int),最簡單的做法是在xml佈局文件中放置ProgressBar空間,然 後再MainActivity中觸發事件後執行incrementProgressBy(int),代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.teachcourse.www.MainActivity" android:orientation="vertical" android:gravity="center" android:id="@+id/onclick_ll" > <!--點擊LinearLayout控件,改變ProgressBar進度--> <ProgressBar android:id="@+id/progressBar_horizontal" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="15dp" android:layout_marginTop="28dp"/> </LinearLayout> |
在Java代碼中非常的簡單,如下圖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class MainActivity extends ActionBarActivity implements View.OnClickListener{ private ProgressBar pb_large; private ProgressBar pb_normal; private ProgressBar pb_small; private ProgressBar pb_horizontal; private int readed; private int progressValue; private LinearLayout onclick_ll; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); Drawable drawable = getResources().getDrawable( R.drawable.progress_bar_states); pb_horizontal.setProgressDrawable(drawable); //設置水平滾動條的樣式 pb_horizontal.setMax( 100 ); //設置進度條的最大值 onclick_ll.setOnClickListener( this ); //LinearLayout添加監視器 } private void initView() { pb_horizontal = (ProgressBar) findViewById(R.id.progressBar_horizontal); onclick_ll=(LinearLayout)findViewById(R.id.onclick_ll); } @Override public void onClick(View v) { pb_horizontal.incrementProgressBy( 5 ); if (pb_horizontal.getProgress()==pb_horizontal.getMax()){ Toast.makeText( this , "進度條已經達到最大值,進度條消失" , Toast.LENGTH_LONG).show(); pb_horizontal.setProgress( 0 ); //當達到最大之後,進度值歸0 } } } |
我們這裏展示的是ProgressBar讀取文件字節流後,展示的讀取進度條,上面的簡單演示就不說了。第一張效果圖:
第三張效果圖:
第四張效果圖
這裏展示文件讀取字節流過程中,ProgressBar進度變化情況,當讀取完成,進度條剛好滿,這在我們實際項目開發中,經常需要使用到,如何成爲我寫這篇文章的原因。
佈局文件progress_horizontal_read_data.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.teachcourse.www.MainActivity" android:orientation="vertical" android:gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/read_data_tv" android:text="@string/read_info" android:scrollbars="vertical" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:lineSpacingExtra="10dp"/> <ProgressBar android:id="@+id/progressBar_horizontal_read_data" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="15dp" android:layout_marginTop="28dp"/> </LinearLayout> |
Java代碼ProgressBarActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | public class ProgressBarActivity extends ActionBarActivity { private ProgressBar pb_horizontal; private int readed; private int progressValue; private TextView mReadProgress_TV; private String mData; private StringBuffer sb; //每次讀取到的字節數據存儲 int length; //標記文件大小 private Thread thread; //讀取文件線程 @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.progress_horizontal_read_data); initView(); Drawable drawable = getResources().getDrawable( R.drawable.progress_bar_states); String path = Environment.getExternalStorageDirectory() + "/ProgressMonitor.txt" ; // ProgressMonitor.txt是我存到sdcard中的一個文件,可以自定義文件名大小位置 final File file = new File(path); if (file.exists()){ length=( int ) file.length(); pb_horizontal.setMax(length); //設置進度條最大值 pb_horizontal.setProgressDrawable(drawable); //自定義進度條樣式 } //啓動一個線程讀取文件內容 thread= new Thread() { @Override public void run() { readFromFile(file.getAbsolutePath()); } }; thread.start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 110 : progressValue += msg.arg1; pb_horizontal.setProgress(progressValue); mReadProgress_TV.setText(sb.toString()); if (progressValue==length){ Toast.makeText(ProgressBarActivity. this , "文件讀取完成" , Toast.LENGTH_LONG).show(); } Log.d( "progressValue-------------->" , progressValue + "" ); break ; } } }; public void readFromFile(String path) { FileInputStream fis; try { fis = new FileInputStream(path); DataInputStream dis = new DataInputStream(fis); sb= new StringBuffer(); byte b[] = new byte [ 10 ]; // 每次讀取1字節 while ((readed = dis.read(b)) != - 1 ) { Message msg = new Message(); msg.arg1 = readed; msg.what = 110 ; mData= new String(b, 0 ,readed); sb.append(mData); handler.sendMessage(msg); try { Thread.sleep( 1000 ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } dis.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } private void initView() { pb_horizontal = (ProgressBar) findViewById(R.id.progressBar_horizontal_read_data); mReadProgress_TV=(TextView)findViewById(R.id.read_data_tv); } } |
這裏使用Handler消息處理,每次讀取到數據後更新進度條,將讀取的字節數據放置在arg1中,在Handler的handleMessage 方法中設置進度值,同時刷新TextView顯示的內容,這裏難就難在如何獲取每次讀取的字節數,然後刷新進度條,在字節流的學習中,我們可以自定義每次 讀取字節大小,File類中可以獲取到文件的總大小,如果不明白字節流的操作的,可以查看《Java開發基礎之文件流操作解析》,最終我們得到上面圖片中 的效果。在這個例子裏面,我們可以將讀取手機卡的文件,換成下載文件,傳輸文件同樣適應。