拖动滑块改变图片透明度
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 }