拖動滑塊改變圖片透明度
1.布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".AndroidProgerssBarActivity" > 7 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_centerHorizontal="true" 12 android:layout_centerVertical="true" 13 android:text="任務完成進度" /> 14 15 <ProgressBar 16 android:id="@+id/bar" 17 style="@android:style/Widget.ProgressBar.Horizontal" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:max="100" /> 21 22 <ProgressBar 23 android:id="@+id/bar2" 24 style="@android:style/Widget.ProgressBar.Horizontal" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:max="100" 28 android:progressDrawable="@drawable/ic_launcher" > 29 </ProgressBar> 30 31 <Button 32 android:id="@+id/btn1" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:text="顯示進度條" /> 36 <Button 37 android:id="@+id/btn2" 38 android:layout_width="match_parent" 39 android:layout_height="wrap_content" 40 android:text="不顯示進度條" /> 41 42 </LinearLayout>
2.邏輯控制
1 package com.example.androidprogerssbar; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.app.Activity; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.Window; 11 import android.widget.Button; 12 import android.widget.ProgressBar; 13 14 public class AndroidProgerssBarActivity extends Activity { 15 16 // 模擬填充長度為100的數組 17 private int[] data = new int[100]; 18 int hasData = 0; 19 // 記錄ProgressBar完成進度 20 int status = 0; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 26 //設置窗口特征,啟用顯示進度的進度條 27 requestWindowFeature(Window.FEATURE_PROGRESS); 28 //設置窗口特征,不啟用顯示進度的進度條 29 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 30 setContentView(R.layout.activity_android_progerss_bar); 31 Button btn1=(Button)this.findViewById(R.id.btn1); 32 Button btn2=(Button)this.findViewById(R.id.btn2); 33 34 final ProgressBar bar = (ProgressBar) this.findViewById(R.id.bar); 35 final ProgressBar bar2 = (ProgressBar) this.findViewById(R.id.bar2); 36 37 btn1.setOnClickListener(new OnClickListener() { 38 39 @Override 40 public void onClick(View arg0) { 41 //顯示不帶進度的進度條 42 setProgressBarIndeterminateVisibility(true); 43 //顯示帶進度的進度條 44 setProgressBarVisibility(true); 45 //設置進度條進度 46 setProgress(4500); 47 } 48 }); 49 50 btn2.setOnClickListener(new OnClickListener() { 51 52 @Override 53 public void onClick(View arg0) { 54 //顯示不帶進度的進度條 55 setProgressBarIndeterminateVisibility(false); 56 //顯示帶進度的進度條 57 setProgressBarVisibility(false); 58 } 59 }); 60 61 // 創建一個負責更新進度的Handler 62 final Handler handler = new Handler() { 63 64 @Override 65 public void handleMessage(Message msg) { 66 // 表明消息是由該程序發送的 67 if (msg.what == 0x111) { 68 bar.setProgress(status); 69 bar2.setProgress(status); 70 } 71 } 72 73 }; 74 75 // 啟動線程執行任務 76 new Thread() { 77 public void run() { 78 while (status < 100) { 79 // 獲得耗時操作的完成百分比 80 status = doWork(); 81 // 發送消息到Handler 82 Message m = new Message(); 83 m.what = 0x111; 84 // 發送消息 85 handler.sendMessage(m); 86 } 87 } 88 }.start(); 89 } 90 91 // 模擬一個耗時操作 92 public int doWork() { 93 data[hasData++] = (int) (Math.random() * 100); 94 try { 95 Thread.sleep(100); 96 } catch (InterruptedException e) { 97 e.printStackTrace(); 98 } 99 return hasData; 100 } 101 102 @Override 103 public boolean onCreateOptionsMenu(Menu menu) { 104 // Inflate the menu; this adds items to the action bar if it is present. 105 getMenuInflater().inflate(R.menu.activity_android_progerss_bar, menu); 106 return true; 107 } 108 109 }

