Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.1/4.2 實現預設螢幕旋轉 180 度的完整改法

編輯:關於 Android 程式設計

要讓 Android 4.1 到 4.2 開機後預設以 180 度方向顯示,需要同時改動屬性、SurfaceFlinger、Java 框架層、最近程式檢視與截圖五個部分。

一、設定屬性值

在 system/build.prop 中加入:

ro.sf.hwrotation= 80

二、設定視窗預設顯示方向

在 frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp 中找到 setDisplayHardware 方法,於 switch 中補上一個分支:

case 180:

displayOrientation = ISurfaceComposer::eOrientation180;

break;

三、設定視窗動畫的旋轉方向

a > 在 frameworks/base/core/java/android/view/Surface.java 中加入兩個方法:

/**

* @hide

*/

public static int getDefaultRotation(){

return android.os.SystemProperties.getInt("ro.sf.hwrotation", 0);//180

}

/**

* @hide

*/

public static int getDefaultRotationIndex(){

int rotation = getDefaultRotation();

switch(rotation){

case 0:

return ROTATION_0;

case 90:

return ROTATION_90;

case 180:

return ROTATION_180;

case 270:

return ROTATION_270;

}

return ROTATION_0;

}

b > 在 frameworks/base/services/java/com/android/server/vm/ScreenRotationAnimation.java 中找到方法 setRotation(Android 4.1)或 setRotationInTransaction(Android 4.2),把:

deltaRotation(rotation,Surface.ROTATION_0);

改為:

deltaRotation(rotation,Surface.getDefaultRotationIndex());

四、修正最近程式檢視的方向

在 frameworks/base/packages/systemui/src/com/android/systemui/RecentsPanelView.java 中,先補上一個成員變數:

private int mThumbnailHeight;//add

接著在 updateVoluesFromResources 方法中加上:

mThumbnailHeight = Math.round(res.getDimension(R.dimen.status_bar_recents_thumbnail_height));//add

然後在 updateThumbnail 方法的 else 分支中,於既有縮放處理之後追加旋轉邏輯:

Matrix scaleMatrix = new Matrix();

float scale = mThumbnailWidth / (float) thumbnail.getWidth();

scaleMatrix.postScale(scale, scale);//setScale

h.thumbnailViewImage.setScaleType(ScaleType.MATRIX);

h.thumbnailViewImage.setImageMatrix(scaleMatrix);

//add

if(Surface.getDefaultRotation() > 0){

Matrix rotateMatrix = new Matrix();

rotateMatrix.setRotate(Surface.getDefaultRotation(),mThumbnailWidth/2,mThumbnailHeight/2);

h.thumbnailViewImage.setImageMatrix(rotateMatrix);

}

//add end

五、修正截圖的方向

在 frameworks/base/packages/systemui/src/com/android/systemui/GlobalScreenshot.java 中找到 takeScreenshot 方法,把:

float degrees = getDegreesForRotation(mDisplay.getRotation());

改為:

int rotation = mDisplay.getRotation();

if(Surface.getDefaultRotation() > 0){

rotation = (rotation + Surface.getDefaultRotationIndex())%4;

}

float degrees = getDegreesForRotation(rotation);

以上五處改完,螢幕旋轉 180 度就完成了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved