如何在Android上检查设备的自然(默认)方向(即获得例如Motorola Charm或Flipout的风景)

我有一个活动显示从相机预览,所以它只需要被设置为风景。 在底部(不pipe设备旋转)我想显示一个文本视图。 我正在使用OrientationEventListener,它从自然的位置给出设备的方向。 我可以实现一个在纵向默认设备上运行良好的解决scheme,但也可以在横向默认设备上工作,我需要注意在这样的设备上运行。 那么问题是如何检查呢?

这种方法可以帮助:

public int getDeviceDefaultOrientation() { WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Configuration config = getResources().getConfiguration(); int rotation = windowManager.getDefaultDisplay().getRotation(); if ( ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE) || ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT)) { return Configuration.ORIENTATION_LANDSCAPE; } else { return Configuration.ORIENTATION_PORTRAIT; } } 

那么,你可以找出@Urboss说的当前方向是什么。 你不能得到这样的布局(风景/人像),所以你必须得到屏幕宽度/高度来检查当前(不pipe它是否改变,但你已经检查了;))位置是风景或肖像:

  DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); wPix = dm.widthPixels; hPix = dm.heightPixels; 

(所以如果旋转angular度为0,并且您使用上面的metrix获得了风景,那么您的设备是默认风景。如果旋转angular度是90度,而且是纵向,则默认设置也是风景,等等)

经过几个小时,试图找出这个问题。 这是不可能的。 但是,您可以做的最接近的事情是将方向设置为“NOSENSOR”

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

这将做的是将您的应用程序设置为设备的自然方向。 此时,您可以使用DisplayMetrics类获取显示的高度和宽度,并计算横向或纵向。 之后,你可以找出是横向还是纵向,然后你可以做到这一点

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_XXXXX);

XXXX是“风景”或“肖像”。

这样做可能不起作用的情况是,如果你有一个滑出式键盘。

由于diyism上面的优秀答案,我还添加了逻辑来检查实际的方向位置(风景右,肖像,左侧景观,人像翻转): 在这里输入图像说明 这里是代码:

 @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { //The coordinate-system is defined relative to the screen of the phone in its default orientation int orientation = 0; float roll=0; float pitch=0; switch (getWindowManager().getDefaultDisplay().getRotation()) { case Surface.ROTATION_0: roll=event.values[2]; pitch=event.values[1]; break; case Surface.ROTATION_90: roll=event.values[1]; pitch=-event.values[2]; break; case Surface.ROTATION_180: roll=-event.values[2]; pitch=-event.values[1]; break; case Surface.ROTATION_270: roll=-event.values[1]; pitch=event.values[2]; break; } if (pitch >= -45 && pitch < 45 && roll >= 45) orientation = 0; else if (pitch < -45 && roll >= -45 && roll < 45) orientation = 1; else if (pitch >= -45 && pitch < 45 && roll < -45) orientation = 2; else if (pitch >= 45 && roll >= -45 && roll < 45 ) orientation = 3; if (m_nOrientation != orientation) { //orientation changed event m_Inst.Debug(LOG_TAG,"onSensorChanged: orientation:" + orientation); m_nOrientation = orientation; // fire event for new notification, or update your interface here } } 

这个代码也张贴在这里: http : //www.pocketmagic.net/? p= 2847

这是我的解决scheme:

 public class ActivityOrientationTest extends Activity { private static final String TAG = "ActivityOrientationTest"; private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; private TextView mTextView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView)findViewById(R.id.text); setDefaultOrientation(); } private void setDefaultOrientation(){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); Display display; display = getWindow().getWindowManager().getDefaultDisplay(); int rotation = display.getRotation(); int width = 0; int height = 0; switch (rotation) { case Surface.ROTATION_0: case Surface.ROTATION_180: Log.i(TAG, "Rotation is: 0 or 180"); width = display.getWidth(); height = display.getHeight(); break; case Surface.ROTATION_90: case Surface.ROTATION_270: Log.i(TAG, "Rotation is: 90 or 270"); width = display.getHeight(); height = display.getWidth(); break; default: break; } if(width > height){ Log.i(TAG, "Natural Orientation is landscape"); mTextView.setText("Natural Orientation is landscape"); mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { Log.i(TAG, "Natural Orientation is portrait"); mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; mTextView.setText("Natural Orientation is portrait"); } setRequestedOrientation(mNaturalOrientation); } 

Display.getRotation()仅在API级别8或更高版本中受支持,因此如果您需要支持较旧的设备,则需要调用Display.getOrientation()。

@Urboss,它不能。 你首先需要知道设备的默认方向,因为这两个方法都会根据它返回变化(我的意思是,如果默认是肖像,你要查找的结果将是1或ROTATE_90 – 因此它是风景,但,如果默认是横向的,结果是0)。

而据我所知,find设备的默认方向并不平凡:(

任何人都可以在这个话题上抛出一些东西?

 public static boolean isDeviceDefaultOrientationLandscape(Activity a) { WindowManager windowManager = (WindowManager) a.getSystemService(Context.WINDOW_SERVICE); Configuration config = a.getResources().getConfiguration(); int rotation = windowManager.getDefaultDisplay().getRotation(); boolean defaultLandsacpeAndIsInLandscape = (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE; boolean defaultLandscapeAndIsInPortrait = (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT; return defaultLandsacpeAndIsInLandscape || defaultLandscapeAndIsInPortrait; } 

我的解决scheme(在HTC愿望和三星Galaxy Tab 10.1testing):

  private class compass_listener implements SensorEventListener {public void onAccuracyChanged(Sensor sensor, int accuracy) {} public void onSensorChanged(SensorEvent event) {float roll=0; float pitch=0; switch (((Activity)context).getWindowManager().getDefaultDisplay().getRotation()) {case Surface.ROTATION_0: roll=event.values[2]; pitch=event.values[1]; break; case Surface.ROTATION_90: roll=event.values[1]; pitch=-event.values[2]; break; case Surface.ROTATION_180: roll=-event.values[2]; pitch=-event.values[1]; break; case Surface.ROTATION_270: roll=-event.values[1]; pitch=event.values[2]; break; } JSONObject json=new JSONObject(); try {json.put("roll", roll); json.put("pitch", pitch); json.put("azimuth", event.values[0]); } catch (JSONException e) {throw new RuntimeException(e); } java_2_js(webview, "intent_compass_change", json); } } 

您可以使用旋转和方向进行定义。 无需使用传感器或任何监听器,只要您希望调用isDefaultLandscape方法。

 private boolean isDefaultLandscape(final Context context) { Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = display.getRotation(); int orientation = context.getResources().getConfiguration().orientation; switch (rotation) { case Surface.ROTATION_180: case Surface.ROTATION_0: { return orientation == Configuration.ORIENTATION_LANDSCAPE; } default: { return orientation == Configuration.ORIENTATION_PORTRAIT; } } } 

解决这个问题非常简单

int orient = this.getResources()。getConfiguration()。orientation;

如果它返回1或2

1是肖像2是风景

可以使用Display.getOrientation()或Display.getRotation() (API级别> = 8)来完成。