如何检查横向模式下设备的方向是顺时针还是逆时针?
How to check if Orientation of device in Landscape Mode is clockwise or anticlockwise?
在我的应用程序中,我使用前端 camera
创建 preview frame
,然后再打开 camera
我想知道 Orientation
中的设备是否在 [=15] =] 是 clockwise
(右侧)或 anticlockwise
(左侧)。
我正在通过
获得Orientation
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
但是告诉我如何检查它是顺时针还是逆时针?
使用context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation()
您只需要注意不同类型的风景,设备通常使用的风景和 other.To 指定 phone 的旋转:
private String clockOrAntiClockWiseRotation(Context context){
final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (rotation) {
case Surface.ROTATION_0:
return "portrait";
case Surface.ROTATION_90:
return "Clockwise";
case Surface.ROTATION_180:
return "reverse portrait";
default:
return "Anti Clockwise";
}
}
查看此代码段:Activity.class
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0){
// clockwise
}else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180){
// anti-clockwise
}
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//portrait
}
不仅要在开始时检查,还应该检查它是否在运行时也发生了变化。
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
MainActivity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//portrait
}
}
在我的应用程序中,我使用前端 camera
创建 preview frame
,然后再打开 camera
我想知道 Orientation
中的设备是否在 [=15] =] 是 clockwise
(右侧)或 anticlockwise
(左侧)。
我正在通过
获得Orientation
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
但是告诉我如何检查它是顺时针还是逆时针?
使用context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation()
您只需要注意不同类型的风景,设备通常使用的风景和 other.To 指定 phone 的旋转:
private String clockOrAntiClockWiseRotation(Context context){
final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (rotation) {
case Surface.ROTATION_0:
return "portrait";
case Surface.ROTATION_90:
return "Clockwise";
case Surface.ROTATION_180:
return "reverse portrait";
default:
return "Anti Clockwise";
}
}
查看此代码段:Activity.class
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0){
// clockwise
}else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180){
// anti-clockwise
}
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//portrait
}
不仅要在开始时检查,还应该检查它是否在运行时也发生了变化。
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
MainActivity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//portrait
}
}