1. 程式人生 > >Android學習:SeekBar實現音量調節

Android學習:SeekBar實現音量調節

SeekBar可以通過滑塊的位置來標識數值----而且拖動條允許使用者拖動滑塊來改變值,因此拖動條通常用於對系統的某種數值進行調節,比如調節音量等。

SeekBar允許使用者改變拖動條的滑塊外觀,改變滑塊外觀通常通過如下屬性來指定:       android:thumb: 指定一個Drawable物件,該物件將自定義滑塊。

為了讓程式能響應拖動條滑塊位置的改變,程式可以考慮為它繫結一個OnSeekBarChangeListener監聽器。

以下是一個使用SeekBar來調節系統音量的例項:

XML程式碼:

 <SeekBar
                android:id="@+id/sound"
                android:layout_width="150px"
                android:layout_height="10px"
                android:max="100"  //設定拖動條最大值
                android:progress="10"   //設定拖動條當前值
                android:progressDrawable="@layout/seekbar_style"  //拖動條樣式
                android:thumb="@layout/thumb" />    //滑塊樣式


 seekbar_style.xml:

<?xml version="1.0" encoding="UTF-8"?>   
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">   
  
    <item android:id="@android:id/background">   
        <shape>   
            <corners android:radius="10dip" />   
            <gradient android:startColor="#ffffffff"  
                android:centerColor="#ff000000" android:endColor="#ff808A87"  
                android:centerY="1" android:angle="270" />   
        </shape>   
    </item>   
  
    <item android:id="@android:id/progress">   
        <clip>   
            <shape>   
                <corners android:radius="10dip" />   
                <gradient android:startColor="#ffffffff"  
                    android:centerColor="#ffFFFF00" android:endColor="#ffAABD00"  
                    android:centerY="1" android:angle="270" />   
            </shape>   
        </clip>   
    </item>   
</layer-list>    


thumb.xml:

<?xml version="1.0" encoding="UTF-8"?>     
<selector xmlns:android="http://schemas.android.com/apk/res/android">           
    <!-- 按下狀態 -->    
    <item       
        android:state_pressed="true"    
        android:drawable="@drawable/thumb_normal"      
        />      
                
    <!-- 普通無焦點狀態 -->    
    <item       
        android:state_focused="false"       
        android:state_pressed="false"     
        android:drawable="@drawable/thumb_normal"  
    />   
  
  
</selector>    

bacon_seekbar.xml:

<layer-list
  xmlns:android="http://schemas.android.com/apk/res/android"
>
  <item
    android:id="@+android:id/background"
    android:drawable="@drawable/thumb_normal" />
  <item
    android:id="@+android:id/SecondaryProgress"
    android:drawable="@drawable/thumb_normal" />
  <item
    android:id="@+android:id/progress"
    android:drawable="@drawable/thumb_normal" />
</layer-list>


JAVA程式碼:

public class PianoActivity extends Activity {
    /** Called when the activity is first created. */
	 private ImageButton imageButton_white1;
	private MediaPlayer mediaPlayer01;
	public  AudioManager audiomanage;
	private TextView mVolume ;  //顯示當前音量
	 public  SeekBar soundBar;
	 private int maxVolume, currentVolume;  



	private int volume=0;  //初始化聲音

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mediaPlayer01 = new MediaPlayer();
      
       

        imageButton_white1=(ImageButton)findViewById(R.id.white1);
        final SeekBar soundBar=(SeekBar)findViewById(R.id.sound);  //音量設定
        mVolume = (TextView)findViewById(R.id.mVolume);  
        audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  


        maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  //獲取系統最大音量
        soundBar.setMax(maxVolume);   //拖動條最高值與系統最大聲匹配
        currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);  //獲取當前值
        soundBar.setProgress(currentVolume);  
        mVolume.setText(currentVolume*100/maxVolume + " %");  
 
        soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() //調音監聽器
        {
        	public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser)
        	{
        		audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);  
        		currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);  //獲取當前值
                soundBar.setProgress(currentVolume);  
        		mVolume.setText(currentVolume*100/maxVolume + " %");  
        	}
        	
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				

			}
        });

TextView的XML沒有給出,需要自己新增。

完成效果: