SeekBar控件

来源:互联网 发布:淘宝代理加盟 编辑:程序博客网 时间:2024/06/07 22:09

什么是SeekBar控件,SeekBar控件其实就是一个高级点的进度条,就像我们在听歌,看电影用的播放器上的进度条一样,是可以拖动的,可以改变进度的一个进度条控件!就是下面这个样子

seekbar对应的方法和属性

android:thumb---seekbar上绘制的thumb(可拖动的那个图标)

view source
print?
1public void setOnSeekBarChangeListener (SeekBar.OnSeekBarChangeListener l)
2  
3//设置一个监听器以接受seekbar进度改变时的通知。同时提供用户在SeekBar上开始和停止触摸手势时的通知。
4  
5//参数 l        SeekBar的通知监听对象
6  
7//参见     SeekBar.OnSeekBarChangeListener

1.在res/layout目录下定义一个布局文件seekbar.xml

view source
print?
01<?xmlversion="1.0"encoding="utf-8"?>
02<ScrollViewxmlns: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如下:

 

view source
print?
001package com.test;
002  
003import android.app.Activity;
004import android.content.Intent;
005import android.os.Bundle;
006import android.os.Handler;
007import android.text.method.ScrollingMovementMethod;
008import android.view.View;
009import android.view.WindowManager;
010import android.view.View.OnClickListener;
011import android.widget.Button;
012import android.widget.ImageView;
013import android.widget.SeekBar;
014import android.widget.SeekBar.OnSeekBarChangeListener;
015import android.widget.TextView;
016  
017public class SeekBarDemo extendsActivity {
018      
019    privateSeekBar  seekBar,seekBar2,seekBar3;
020    privateTextView textview,textview2;
021    private ImageView  imageView;
022    //标记是否需要刷新
023    privateboolean flag=true;
024    privateHandler hangler=newHandler();
025    ////
026    private int []  arrayImage =newint[]{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    protectedvoid 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 = newOnSeekBarChangeListener() {
054          
055        @Override
056        publicvoid onStopTrackingTouch(SeekBar seekBar) {
057            // TODO Auto-generated method stub
058              
059        }
060          
061        @Override
062        publicvoid onStartTrackingTouch(SeekBar seekBar) {
063            // TODO Auto-generated method stub
064              
065        }
066          
067        @Override
068        publicvoid onProgressChanged(SeekBar seekBar,int progress,
069                booleanfromUser) {
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    privatevoid setScreenBrightness(floatnum) {   // 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=newOnSeekBarChangeListener() {
092          
093        @Override
094        //当游标移动停止的时候调用该方法
095        publicvoid onStopTrackingTouch(SeekBar seekBar) {
096        //设置标记为需要刷新
097            flag=true;
098       //创建时就开始自动更新该拖动条
099            refresh();
100        }
101          
102          
103        @Override
104        //当游标开始移动时调用该方法
105        publicvoid onStartTrackingTouch(SeekBar seekBar) {
106        //停止刷新
107            flag=false;
108        }
109          
110        @Override
111        //当进度条游标被改变或者进度条改变时调用该方法
112        publicvoid onProgressChanged(SeekBar seekBar,int progress,
113                booleanfromUser) {
114            //更改textView的内容
115            textview.setText("进度为:"+progress+"%");
116              
117        }
118          
119    };
120    //该方法自动更新该拖动条
121    privatevoid refresh() {
122        newThread(new Runnable() {
123              
124            @Override
125            publicvoid 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(newRunnable() {
140                          
141                        @Override
142                        publicvoid run() {
143                            // 重新设置进度条当前的值
144                            seekBar.setProgress(seekBar.getProgress()+1);
145                              
146                        }
147                    });
148                }
149            }
150        }).start();
151       }
152}

 三:执行效果如下:

 

原创粉丝点击