2011-01-21 58 views
2

我想使它成爲我的應用程序永遠不會進入肖像模式,但能夠在2個橫向屏幕視圖之間切換。我知道這很容易在薑餅(2.3)來完成,但我有麻煩做手工爲Android的其他版本,我的代碼如下:使用Display.getRotation()的屏幕旋轉

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     int orientation = display.getRotation(); 
     if (orientation == 1) { 
      /* The device is rotated to the left. */ 
      Log.v("Left", "Rotated Left"); 
     } else if (orientation == 3) { 
      /* The device is rotated to the right. */ 
     Log.v("Right", "Rotated Right"); 
     } else { 

     } 

我的問題是我怎麼翻轉X和屏幕視圖的y軸取決於檢測到的旋轉?我如何獲得它們以扭轉它們?

回答

2

據我所知在2.3之前不支持倒排佈局。所以除非你用自定義的SurfaceView繪製屏幕,​​否則你不能用標準的小部件來做。使用表面視圖,您只需在渲染之前轉換整個畫布即可。

此外,你應該要想知道當前的方向(沒有魔法numuber)使用常量,看到Configuration class API documentation

if (orientation==Configuration.ORIENTATION_LANDSCAPE) 
+0

必須有辦法手動設置x軸和y軸? android操作系統如何知道自己做它? – SamRowley 2011-01-21 10:40:29

1

getRotation類 '顯示' 在https://developer.android.com/reference/android/view/Display.html#getRotation()

getRotation回報解釋:

Surface.ROTATION_0 (no rotation), 
Surface.ROTATION_90, 
Surface.ROTATION_180, 
or Surface.ROTATION_270. 

例如,如果設備具有自然高大的屏幕,並且用戶已將其轉到其側以進入al和風向,這裏返回的值可能是Surface.ROTATION_90或Surface.ROTATION_270,具體取決於它轉向的方向。該角度是屏幕上繪製圖形的旋轉,這與設備的物理旋轉方向相反。例如,如果設備逆時針旋轉90度,則補償渲染將順時針旋轉90度,因此返回值將爲Surface.ROTATION_90。