zoukankan      html  css  js  c++  java
  • Android学习:SeekBar实现音量调节

            Android学习:SeekBar实现音量调节       

    412人阅读评论(0)收藏举报

    SeekBar可以通过滑块的位置来标识数值----而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等。

    SeekBar允许用户改变拖动条的滑块外观,改变滑块外观通常通过如下属性来指定:       android:thumb: 指定一个Drawable对象,该对象将自定义滑块。

    为了让程序能响应拖动条滑块位置的改变,程序可以考虑为它绑定一个OnSeekBarChangeListener监听器。

    以下是一个使用SeekBar来调节系统音量的实例:

    XML代码:

    1. <SeekBar 
    2.                android:id="@+id/sound" 
    3.                android:layout_width="150px" 
    4.                android:layout_height="10px" 
    5.                android:max="100"  //设置拖动条最大值 
    6.                android:progress="10"   //设置拖动条当前值 
    7.                android:progressDrawable="@layout/seekbar_style"  //拖动条样式 
    8.                android:thumb="@layout/thumb"/>    //滑块样式 

    seekbar_style.xml:

    1. <?xmlversion="1.0"encoding="UTF-8"?>    
    2. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">    
    3.    
    4.     <itemandroid:id="@android:id/background">    
    5.         <shape>    
    6.             <cornersandroid:radius="10dip"/>    
    7.             <gradientandroid:startColor="#ffffffff"   
    8.                 android:centerColor="#ff000000"android:endColor="#ff808A87"   
    9.                 android:centerY="1"android:angle="270"/>    
    10.         </shape>    
    11.     </item>    
    12.    
    13.     <itemandroid:id="@android:id/progress">    
    14.         <clip>    
    15.             <shape>    
    16.                 <cornersandroid:radius="10dip"/>    
    17.                 <gradientandroid:startColor="#ffffffff"   
    18.                     android:centerColor="#ffFFFF00"android:endColor="#ffAABD00"   
    19.                     android:centerY="1"android:angle="270"/>    
    20.             </shape>    
    21.         </clip>    
    22.     </item>    
    23. </layer-list>     

    thumb.xml:

    1. <?xmlversion="1.0"encoding="UTF-8"?>      
    2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">            
    3.     <!-- 按下状态 -->     
    4.     <item        
    5.         android:state_pressed="true"     
    6.         android:drawable="@drawable/thumb_normal"       
    7.         />       
    8.                  
    9.     <!-- 普通无焦点状态 -->     
    10.     <item        
    11.         android:state_focused="false"        
    12.         android:state_pressed="false"      
    13.         android:drawable="@drawable/thumb_normal"   
    14.     />    
    15.    
    16.    
    17. </selector>     

    bacon_seekbar.xml:

    1. <layer-list 
    2.   xmlns:android="http://schemas.android.com/apk/res/android" 
    3. > 
    4.   <item 
    5.     android:id="@+android:id/background" 
    6.     android:drawable="@drawable/thumb_normal"/> 
    7.   <item 
    8.     android:id="@+android:id/SecondaryProgress" 
    9.     android:drawable="@drawable/thumb_normal"/> 
    10.   <item 
    11.     android:id="@+android:id/progress" 
    12.     android:drawable="@drawable/thumb_normal"/> 
    13. </layer-list> 

    JAVA代码:

    1. publicclass PianoActivity extends Activity { 
    2.     /** Called when the activity is first created. */ 
    3.      private ImageButton imageButton_white1; 
    4.     private MediaPlayer mediaPlayer01; 
    5.     public  AudioManager audiomanage; 
    6.     private TextView mVolume ;  //显示当前音量 
    7.      public  SeekBar soundBar; 
    8.      privateint maxVolume, currentVolume;   
    9.  
    10.  
    11.  
    12.     privateint volume=0//初始化声音 
    13.  
    14.     @Override 
    15.     publicvoid onCreate(Bundle savedInstanceState) { 
    16.         super.onCreate(savedInstanceState); 
    17.         setContentView(R.layout.main); 
    18.         mediaPlayer01 = new MediaPlayer(); 
    19.        
    20.         
    21.  
    22.         imageButton_white1=(ImageButton)findViewById(R.id.white1); 
    23.         final SeekBar soundBar=(SeekBar)findViewById(R.id.sound);  //音量设置 
    24.         mVolume = (TextView)findViewById(R.id.mVolume);   
    25.         audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);   
    26.  
    27.  
    28.         maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  //获取系统最大音量 
    29.         soundBar.setMax(maxVolume);   //拖动条最高值与系统最大声匹配 
    30.         currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);  //获取当前值 
    31.         soundBar.setProgress(currentVolume);   
    32.         mVolume.setText(currentVolume*100/maxVolume + " %");   
    33.   
    34.         soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() //调音监听器 
    35.         { 
    36.             publicvoid onProgressChanged(SeekBar arg0,int progress,boolean fromUser) 
    37.             { 
    38.                 audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);   
    39.                 currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);  //获取当前值 
    40.                 soundBar.setProgress(currentVolume);   
    41.                 mVolume.setText(currentVolume*100/maxVolume + " %");   
    42.             } 
    43.              
    44.             @Override 
    45.             publicvoid onStartTrackingTouch(SeekBar seekBar) { 
    46.                 // TODO Auto-generated method stub 
    47.                  
    48.             } 
    49.             @Override 
    50.             publicvoid onStopTrackingTouch(SeekBar seekBar) { 
    51.                 // TODO Auto-generated method stub 
    52.                  
    53.  
    54.             } 
    55.         }); 
  • 相关阅读:
    JDBC和Ibatis中的Date,Time,Timestamp处理
    Spring Boot 配置定时任务
    SpringBoot Caused by: java.lang.NoClassDefFoundError: org/apache/tomcat/util/descriptor/tld/TldParser
    spring boot 使用thymeleaf模版 报错:org.thymeleaf.exceptions.TemplateInputException
    mybatis 报错: Invalid bound statement (not found)
    spring boot
    通过枚举enum实现单例
    lucene Filter过滤器
    javaweb url
    mysql 报错:java.lang.OutOfMemoryError: Java heap space
  • 原文地址:https://www.cnblogs.com/shanshan520/p/2441428.html
Copyright © 2011-2022 走看看