Android SystemUI在导航栏添加音量加减按钮

有些Android设备没有实体的音量调节按钮,或者从保护实体按键的角度考虑,就需要在导航栏的虚拟按键中添加音量加减调节按键。

效果如下图所示:


实现过程如下:

1.首先在SystemUI中添加音量加减的资源文件,路径如下:

frameworks/base/packages/SystemUI/res/

将图片放入对应的drawable文件夹,包括音量+,和音量-,见上图。


2.修改导航栏的布局文件,路径:

frameworks/base/packages/SystemUI/res/

在对应的layout文件夹中找到navigation_bar.xml文件进行修改:

在返回键前面添加“音量减”,返回键的布局:

[html]  view plain copy
  1. <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"  
  2.                 android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"  
  3.                 android:layout_height="match_parent"  
  4.                 android:src="@drawable/ic_sysbar_back"  
  5.                 systemui:keyCode="4"  
  6.                 android:layout_weight="0"  
  7.                 systemui:glowBackground="@drawable/ic_sysbar_highlight"  
  8.                 android:contentDescription="@string/accessibility_back"  
  9.                 />  
音量减的布局如下,这里先把Visibility定义为Gone,然后在代码中控制是否显示:

[html]  view plain copy
  1. <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sub"  
  2.                 android:src="@drawable/sub_normal"  
  3.                 android:layout_width="@dimen/navigation_key_width"  
  4.                 android:layout_height="match_parent"  
  5.                 android:layout_weight="0"  
  6.                 systemui:keyCode="302"  
  7.                 systemui:glowBackground="@drawable/ic_sysbar_highlight"  
  8.                 android:visibility="gone"/>    

“音量加”添加到“最近应用”之后,最近应用的布局:

[html]  view plain copy
  1. <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"  
  2.                 android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"  
  3.                 android:layout_height="match_parent"  
  4.                 android:src="@drawable/ic_sysbar_recent"  
  5.                 android:layout_weight="0"  
  6.                 systemui:glowBackground="@drawable/ic_sysbar_highlight"  
  7.                 android:contentDescription="@string/accessibility_recent"  
  8.                 />  

音量加的布局:

[html]  view plain copy
  1. <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/add"  
  2.                 android:src="@drawable/add_normal"  
  3.                 android:layout_width="@dimen/navigation_key_width"  
  4.                 android:layout_height="match_parent"  
  5.                 android:layout_weight="0"  
  6.                 systemui:keyCode="301"  
  7.                 systemui:glowBackground="@drawable/ic_sysbar_highlight"  
  8.                 android:visibility="gone"/>   

3.接着修改代码逻辑,文件路径:

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

在private void prepareNavigationBarView() {……}函数中添加显示音量加减的代码:

[java]  view plain copy
  1. mNavigationBarView.getAddVolume().setVisibility(View.VISIBLE);  
  2.         mNavigationBarView.getSubVolume().setVisibility(View.VISIBLE);  

对应的函数getAddVolume()和getAddVolume()要在

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java

中实现:

[java]  view plain copy
  1. public View getAddVolume(){  
  2.        return mCurrentView.findViewById(R.id.add);  
  3.     }  
  4.   
  5.     public View getSubVolume(){  
  6.         return mCurrentView.findViewById(R.id.sub);  
  7.     }   

最后就是功能实现了,在

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

中添加监听函数:

[java]  view plain copy
  1. private View.OnTouchListener mAddVolumeOnTouchListener = new View.OnTouchListener() {  
  2.       public boolean onTouch(View v, MotionEvent ev) {  
  3.       final int action = ev.getAction();  
  4.                 switch(action) {  
  5.                 case MotionEvent.ACTION_DOWN:  
  6.                    is_down = true;  
  7.                    Adjust_Volume(true);  
  8.                    maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);  
  9.                    break;  
  10.                 case MotionEvent.ACTION_MOVE:  
  11.                    is_down = true;  
  12.                    maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);  
  13.     //             maddHandler.removeCallbacks(maddRun);  
  14.                    break;  
  15.                 case MotionEvent.ACTION_UP:  
  16.                    is_down = false;  
  17.                    maddHandler.removeCallbacks(maddRun);  
  18.                    break;  
  19.                   
  20.          }   
  21.          return true;  
  22.       }  
  23.     };  
  24.   
  25.   
  26.     private View.OnTouchListener mSubVolumeOnTouchListener = new View.OnTouchListener() {  
  27.            public boolean onTouch(View v, MotionEvent ev) {  
  28.            final int action = ev.getAction();  
  29.             int x, y;  
  30.             //int mCode = ev.getAction();  
  31.   
  32.                 switch (action) {  
  33.                 case MotionEvent.ACTION_DOWN:  
  34.                   is_down = true;  
  35.                   Adjust_Volume(false);  
  36.                   msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY * 2);  
  37.                   break;  
  38.                 case MotionEvent.ACTION_MOVE:  
  39.                     
  40.                   is_down = true;  
  41.                   msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY * 2);  
  42.                   //msubHandler.removeCallbacks(msubRun);  
  43.                   break;  
  44.                 case MotionEvent.ACTION_UP:  
  45.                   is_down = false;  
  46.                   msubHandler.removeCallbacks(msubRun);  
  47.                   break;  
  48.             }  
  49.             return true;  
  50.         }  
  51.     };  
  52.   
  53.     public void Adjust_Volume(boolean opition){  
  54.             AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);  
  55.             if (audioManager != null) {  
  56.                 //  
  57.                  // Adjust the volume in on key down since it is more  
  58.                  // responsive to the user.  
  59.                  //  
  60.                     if(opition){  
  61.                             audioManager.adjustSuggestedStreamVolume(  
  62.                             AudioManager.ADJUST_RAISE,  
  63.                            AudioManager.USE_DEFAULT_STREAM_TYPE,  
  64.                            AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);  
  65.                     }else{  
  66.                             audioManager.adjustSuggestedStreamVolume(  
  67.                          AudioManager.ADJUST_LOWER,  
  68.                         AudioManager.USE_DEFAULT_STREAM_TYPE,  
  69.                         AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);  
  70.                     }  
  71.             }  
  72.     }  

你可能感兴趣的:(Android SystemUI在导航栏添加音量加减按钮)