Android中通過按鍵旋轉屏幕

Q5有個功能,通過長按menu按鍵,可以旋轉屏幕方向(0°或者90°),這個功能有時蠻有用的,下面來看看是如何實現的:

1 修改按鍵處理程序
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java

函數
    public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down, 
            int repeatCount, int flags)
在處理菜單鍵的地方
if (code == KeyEvent.KEYCODE_MENU) {
            final int chordBug = KeyEvent.META_SHIFT_ON;

            if (down && repeatCount == 0) {
                if (mEnableShiftMenuBugReports && (metaKeys & chordBug) == chordBug) {
                    Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
                    mContext.sendOrderedBroadcast(intent, null);
                    return true;
                } else if (SHOW_PROCESSES_ON_ALT_MENU &&
                        (metaKeys & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
                    Intent service = new Intent();
                    service.setClassName(mContext, "com.android.server.LoadAverageService");
                    ContentResolver res = mContext.getContentResolver();
                    boolean shown = Settings.System.getInt(
                            res, Settings.System.SHOW_PROCESSES, 0) != 0;
                    if (!shown) {
                        mContext.startService(service);
                    } else {
                        mContext.stopService(service);
                    }
                    Settings.System.putInt(
                            res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1);
                    return true;
                }

}
//上面是原來的內容,下面是添加的新內容
else if (down && repeatCount == 20 && MenuKeyUp && (!keyguardOn)) { 
//如果按下Menu鍵一定時間,擡起時執行此段函數
                MenuKeyUp = false;
   try {
                int ro = mWindowManager.getRotation(); //獲取當前方向
                
                    if( ro == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ) {
                        ro = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    } else {
                        ro = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    }
                } 
   catch (RemoteException e) {
                    Log.v(TAG, "!!! getRotation fail !!!");
                }

                try {
                    //旋轉屏幕
                    mWindowManager.setRotation(ro, true, Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
//最後可跟不同的參數,可實現一些旋轉效果
                } 
   catch (RemoteException e) {
                    Log.v(TAG, "!!! mWindowManager.setRotation fail !!!");
                }
                return true;
            }
            if(!down) {
                MenuKeyUp = true;            
            }
}


2 修改實現選擇的函數
/frameworks/base/services/java/com/android/server/WindowManagerService.java

找到該函數
    public boolean setRotationUncheckedLocked(int rotation, int animFlags)

將以下妨礙選擇的內容註釋掉
        //rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
        //        mRotation, mDisplayEnabled);


3、當然也可以新作一個rotate鍵來選擇屏幕,以下是引用代碼

+ } else if (code == KeyEvent.KEYCODE_ROTATE) { 
+ // ROTATE KEY pressed 
+ if (down) { 
+ mButtonPushFlg = true; 

+ try { 
+ int ro = mWindowManager.getRotation(); // Orientation vertical 
+ if (ro == 3 ) { 
+ mWindowManager.setRotation (Surface.ROTATION_0,true,mFancyRotationAnimation); //Orientation 
landscape 
+ } else { 
+ mWindowManager.setRotation 
(Surface.ROTATION_270,true,mFancyRotationAnimation); //Orientation 
portlate 
+ } 
+ } catch (RemoteException e) { 
+ // Igbore 
+ Log.i("info", "Rotation failed "); 
+ } 
+ } 
+ return true; 
}

 

OK,重新編譯後,長按Menu鍵即可實現屏幕旋轉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章