SeekBar控件

什麼是SeekBar控件,SeekBar控件其實就是一個高級點的進度條,就像我們在聽歌,看電影用的播放器上的進度條一樣,是可以拖動的,可以改變進度的一個進度條控件!就是下面這個樣子

seekbar對應的方法和屬性

android:thumb---seekbar上繪製的thumb(可拖動的那個圖標)

1 public void setOnSeekBarChangeListener (SeekBar.OnSeekBarChangeListener l)
2   
3 //設置一個監聽器以接受seekbar進度改變時的通知。同時提供用戶在SeekBar上開始和停止觸摸手勢時的通知。
4   
5 //參數 l        SeekBar的通知監聽對象
6   
7 //參見     SeekBar.OnSeekBarChangeListener

1.在res/layout目錄下定義一個佈局文件seekbar.xml

01 <?xml version="1.0" encoding="utf-8"?>
02 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="fill_parent"
04     android:layout_height="fill_parent"
05     >
06     <LinearLayout 
07         android:layout_width="fill_parent"
08         android:layout_height="fill_parent"
09          android:orientation="vertical"
10         >
11             <TextView 
12                 android:id="@+id/text"
13                 android:layout_width="fill_parent" 
14                 android:layout_height="wrap_content" 
15                 android:text="我是SeekBar=進度爲:"
16                 android:lines="2"
17                 android:textColor="#11ddff"
18                />
19             <SeekBar 
20                  android:id="@+id/seekbar_btn_id"
21                  android:layout_width="fill_parent"
22                  android:layout_height="wrap_content"
23              />
24             <ImageView 
25                 android:id="@+id/seekbar_imageview_id"
26                 android:layout_width="fill_parent" 
27                 android:layout_height="wrap_content" 
28                 android:src="@drawable/gallery_01"
29                 android:lines="6"
30                 android:textColor="#11ddff"
31                 />
32             <LinearLayout 
33                     android:layout_width="fill_parent"
34                     android:layout_height="fill_parent"
35                      android:orientation="horizontal">
36                     <TextView 
37                        android:layout_width="wrap_content" 
38                         android:layout_height="wrap_content"
39                         android:text="拖到切換圖片"
40                         />
41                     <SeekBar 
42                      android:id="@+id/seekbar_hand_id"
43                      android:layout_width="fill_parent"
44                      android:layout_height="wrap_content"
45                      />
46              </LinearLayout>
47              <LinearLayout 
48                     android:layout_width="fill_parent"
49                     android:layout_height="fill_parent"
50                      android:orientation="horizontal">
51                        <TextView 
52                         android:id="@+id/seekbar_liang_id"
53                         android:layout_width="wrap_content" 
54                         android:layout_height="wrap_content" 
55                         android:text="改變屏幕亮度"
56                         android:textColor="#11ddff"
57                        />
58                         <SeekBar 
59                              android:id="@+id/seekbar_hang_id1"
60                              android:layout_width="fill_parent"
61                              android:layout_height="wrap_content"
62                          />
63              </LinearLayout>
64       </LinearLayout>
65 </ScrollView>

2.代碼文件SeekBarDemo.java如下:

 

001 package com.test;
002   
003 import android.app.Activity;
004 import android.content.Intent;
005 import android.os.Bundle;
006 import android.os.Handler;
007 import android.text.method.ScrollingMovementMethod;
008 import android.view.View;
009 import android.view.WindowManager;
010 import android.view.View.OnClickListener;
011 import android.widget.Button;
012 import android.widget.ImageView;
013 import android.widget.SeekBar;
014 import android.widget.SeekBar.OnSeekBarChangeListener;
015 import android.widget.TextView;
016   
017 public class SeekBarDemo extends Activity {
018       
019     private SeekBar  seekBar,seekBar2,seekBar3;
020     private TextView textview,textview2;
021     private  ImageView  imageView;
022     //標記是否需要刷新
023     private boolean flag=true;
024     private Handler hangler=new Handler();
025     ////
026     private  int []  arrayImage =new int[]{R.drawable.gallery_01,
027             R.drawable.gallery_02,R.drawable.gallery_03,R.drawable.gallery_04,R.drawable.gallery_05,R.drawable.gallery_06};
028       
029     @Override
030     protected void onCreate(Bundle savedInstanceState) {
031         // TODO Auto-generated method stub
032         super.onCreate(savedInstanceState);
033         setContentView(R.layout.seekbar);
034           
035         seekBar=(SeekBar)findViewById(R.id.seekbar_btn_id);
036         seekBar2=(SeekBar)findViewById(R.id.seekbar_hand_id);//第二個
037         seekBar2=(SeekBar)findViewById(R.id.seekbar_hand_id);//第二個
038         seekBar3=(SeekBar)findViewById(R.id.seekbar_hang_id1);//第san個
039         textview=(TextView)findViewById(R.id.text);
040         //設置拖動條的最大值,其將爲該拖動條顯示的基數
041         seekBar.setMax(100);
042         //爲該方法seekbar註冊一個監聽,當seekbar發生改變時調用1中的對應方法
043         seekBar.setOnSeekBarChangeListener(onSeekbar);
044         imageView = (ImageView)findViewById(R.id.seekbar_imageview_id);
045         refresh();
046         ///////
047          this.seekBar2.setMax(arrayImage.length);
048         this.seekBar3.setMax(100);
049         this.seekBar2.setOnSeekBarChangeListener(seekbar);
050         this.seekBar3.setOnSeekBarChangeListener(seekbar);
051     }
052     //第二個seekBar
053     private  OnSeekBarChangeListener  seekbar = new OnSeekBarChangeListener() {
054           
055         @Override
056         public void onStopTrackingTouch(SeekBar seekBar) {
057             // TODO Auto-generated method stub
058               
059         }
060           
061         @Override
062         public void onStartTrackingTouch(SeekBar seekBar) {
063             // TODO Auto-generated method stub
064               
065         }
066           
067         @Override
068         public void onProgressChanged(SeekBar seekBar, int progress,
069                 boolean fromUser) {
070             // TODO Auto-generated method stub
071             if(seekBar.getId()==R.id.seekbar_hand_id)
072             {
073                 imageView.setImageResource(arrayImage[seekBar2.getProgress()]);
074             }
075             if(seekBar.getId()==R.id.seekbar_hang_id1)
076             {
077                 setScreenBrightness((float) seekBar
078                         .getProgress() / 100);
079             }
080           
081               
082         }
083     };
084     //表示屏幕亮度的方法
085     private void setScreenBrightness(float num) {   // 0 ~ 1表示亮度
086         WindowManager.LayoutParams layoutParams = super.getWindow().getAttributes() ;   // 取得屏幕的屬性
087         layoutParams.screenBrightness = num ;   // 設置屏幕亮度
088         super.getWindow().setAttributes(layoutParams) ; // 重新設置窗口的屬性
089     }
090     //第一個sekbar
091     private  OnSeekBarChangeListener  onSeekbar=new OnSeekBarChangeListener() {
092           
093         @Override
094         //當遊標移動停止的時候調用該方法
095         public void onStopTrackingTouch(SeekBar seekBar) {
096         //設置標記爲需要刷新
097             flag=true;
098        //創建時就開始自動更新該拖動條
099             refresh();
100         }
101           
102           
103         @Override
104         //當遊標開始移動時調用該方法
105         public void onStartTrackingTouch(SeekBar seekBar) {
106         //停止刷新
107             flag=false;
108         }
109           
110         @Override
111         //當進度條遊標被改變或者進度條改變時調用該方法
112         public void onProgressChanged(SeekBar seekBar, int progress,
113                 boolean fromUser) {
114             //更改textView的內容
115             textview.setText("進度爲:"+progress+"%");
116               
117         }
118           
119     };
120     //該方法自動更新該拖動條
121     private void refresh() {
122         new Thread(new Runnable() {
123               
124             @Override
125             public void run() {
126                 // TODO Auto-generated method stub
127                 //當進度不到1000,就更新status
128                 while(flag && seekBar.getProgress()<100)
129                 {
130                     try {
131                         //暫停1秒
132                         Thread.sleep(1000);
133                     } catch (InterruptedException e) {
134                         // TODO: handle exception
135                         e.printStackTrace();
136                     }
137                     //將一個Runnable對象添加到消息隊列當中,
138                     //並且當執行到該對象時執行run()方法
139                     hangler.post(new Runnable() {
140                           
141                         @Override
142                         public void run() {
143                             // 重新設置進度條當前的值
144                             seekBar.setProgress(seekBar.getProgress()+1);
145                               
146                         }
147                     });
148                 }
149             }
150         }).start();
151        }
152 }

 三:執行效果如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章