Android SystemUI原始碼分析和修改

來源:互聯網
上載者:User

標籤:

1.在導覽列中添加音量加減button

一些Android音量大小button。或者從保護實體按鍵的角度考慮,就須要在導覽列的虛擬按鍵中加入音量加減調節按鍵。

效果例如以所看到的:


實現步驟例如以下:

1.首先在SystemUI中加入音量加減的資源檔。路徑例如以下:

frameworks/base/packages/SystemUI/res/

將圖片放入相應的drawable目錄,包含音量+。和音量-,見。


2.改動導覽列的布局檔案。路徑:

frameworks/base/packages/SystemUI/res/

在相應的layout目錄中找到navigation_bar.xml檔案進行改動:

在返回鍵前面加入“音量減”。返回鍵的布局:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"                android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"                android:layout_height="match_parent"                android:src="@drawable/ic_sysbar_back"                systemui:keyCode="4"                android:layout_weight="0"                systemui:glowBackground="@drawable/ic_sysbar_highlight"                android:contentDescription="@string/accessibility_back"                />
音量減的布局例如以下。這裡先把Visibility定義為Gone,然後在代碼中控制是否顯示:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sub"                android:src="@drawable/sub_normal"                android:layout_width="@dimen/navigation_key_width"                android:layout_height="match_parent"                android:layout_weight="0"                systemui:keyCode="302"                systemui:glowBackground="@drawable/ic_sysbar_highlight"                android:visibility="gone"/>  

“音量加”加入到“近期應用”之後,近期應用的布局:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"                android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"                android:layout_height="match_parent"                android:src="@drawable/ic_sysbar_recent"                android:layout_weight="0"                systemui:glowBackground="@drawable/ic_sysbar_highlight"                android:contentDescription="@string/accessibility_recent"                />

音量加的布局:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/add"                android:src="@drawable/add_normal"                android:layout_width="@dimen/navigation_key_width"                android:layout_height="match_parent"                android:layout_weight="0"                systemui:keyCode="301"                systemui:glowBackground="@drawable/ic_sysbar_highlight"                android:visibility="gone"/> 

3.接著改動代碼邏輯,檔案路徑:

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

在private void prepareNavigationBarView() {……}函數中加入顯示音量加減的代碼:

mNavigationBarView.getAddVolume().setVisibility(View.VISIBLE);    mNavigationBarView.getSubVolume().setVisibility(View.VISIBLE);

相應的函數getAddVolume()和getAddVolume()要在

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

中實現:

public View getAddVolume(){       return mCurrentView.findViewById(R.id.add);    }    public View getSubVolume(){        return mCurrentView.findViewById(R.id.sub);    } 

最後就是功能實現了。在

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

中加入監聽函數:

private View.OnTouchListener mAddVolumeOnTouchListener = new View.OnTouchListener() {      public boolean onTouch(View v, MotionEvent ev) {      final int action = ev.getAction();                switch(action) {                case MotionEvent.ACTION_DOWN:                   is_down = true;                   Adjust_Volume(true);                   maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);                   break;                case MotionEvent.ACTION_MOVE:                   is_down = true;                   maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);    //             maddHandler.removeCallbacks(maddRun);                   break;                case MotionEvent.ACTION_UP:                   is_down = false;                   maddHandler.removeCallbacks(maddRun);                   break;                         }          return true;      }    };    private View.OnTouchListener mSubVolumeOnTouchListener = new View.OnTouchListener() {           public boolean onTouch(View v, MotionEvent ev) {           final int action = ev.getAction();            int x, y;            //int mCode = ev.getAction();                switch (action) {                case MotionEvent.ACTION_DOWN:                  is_down = true;                  Adjust_Volume(false);                  msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY * 2);                  break;                case MotionEvent.ACTION_MOVE:                                    is_down = true;                  msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY * 2);                  //msubHandler.removeCallbacks(msubRun);                  break;                case MotionEvent.ACTION_UP:                  is_down = false;                  msubHandler.removeCallbacks(msubRun);                  break;            }            return true;        }    };    public void Adjust_Volume(boolean opition){            AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);            if (audioManager != null) {                //                 // Adjust the volume in on key down since it is more                 // responsive to the user.                 //                    if(opition){                            audioManager.adjustSuggestedStreamVolume(                            AudioManager.ADJUST_RAISE,                           AudioManager.USE_DEFAULT_STREAM_TYPE,                           AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);                    }else{                            audioManager.adjustSuggestedStreamVolume(                         AudioManager.ADJUST_LOWER,                        AudioManager.USE_DEFAULT_STREAM_TYPE,                        AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);                    }            }    }




轉載請註明出處:周木水的CSDN部落格 http://blog.csdn.net/zhoumushui



著作權聲明:原創文章,轉載請註明出處:http://blog.csdn.net/zhoumushui

Android SystemUI原始碼分析和修改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.