Android 即使屏幕方向为Sensor横向,也不会触发反向横向
我在清单中添加了screenorientation=sensorscape:Android 即使屏幕方向为Sensor横向,也不会触发反向横向,android,screen-orientation,landscape-portrait,Android,Screen Orientation,Landscape Portrait,我在清单中添加了screenorientation=sensorscape: <activity android:name=".WorkoutActivity" android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden|screenSize"> </activity> 在onCo
<activity
android:name=".WorkoutActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
在onConfigurationChanged中,我调用了initOrientation()方法
此外,当我尝试以下代码时,方向并没有停止旋转
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_0 ){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if(rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_180){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
这是一个常见的问题。
我正在使用OrientationEventListener
private void initOrientation() {
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mLastRotation = mWindowManager.getDefaultDisplay().getRotation();
myOrientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI){
@Override
public void onOrientationChanged(int orientation) {
// O.Log.d(TAG, "ORIENTATION: " + orientation);
Display display = mWindowManager.getDefaultDisplay();
int rotation = display.getRotation();
if (((rotation == Surface.ROTATION_90 && mLastRotation == Surface.ROTATION_270)
|| (rotation == Surface.ROTATION_270 && mLastRotation == Surface.ROTATION_90)
|| (rotation == Surface.ROTATION_0 && mLastRotation == Surface.ROTATION_180)
|| (rotation == Surface.ROTATION_180 && mLastRotation == Surface.ROTATION_0))) {
// do something
mLastRotation = rotation;
}
}
};
if (myOrientationEventListener.canDetectOrientation()){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
myOrientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
// finish();
}
}
尝试以下操作(代码冻结UI旋转):
这是一个常见的问题。
我正在使用OrientationEventListener
private void initOrientation() {
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mLastRotation = mWindowManager.getDefaultDisplay().getRotation();
myOrientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI){
@Override
public void onOrientationChanged(int orientation) {
// O.Log.d(TAG, "ORIENTATION: " + orientation);
Display display = mWindowManager.getDefaultDisplay();
int rotation = display.getRotation();
if (((rotation == Surface.ROTATION_90 && mLastRotation == Surface.ROTATION_270)
|| (rotation == Surface.ROTATION_270 && mLastRotation == Surface.ROTATION_90)
|| (rotation == Surface.ROTATION_0 && mLastRotation == Surface.ROTATION_180)
|| (rotation == Surface.ROTATION_180 && mLastRotation == Surface.ROTATION_0))) {
// do something
mLastRotation = rotation;
}
}
};
if (myOrientationEventListener.canDetectOrientation()){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
myOrientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
// finish();
}
}
尝试以下操作(代码冻结UI旋转):
嗨,维亚切斯拉夫,你能不能请我根据你的回答对这个问题进行编辑?你能进一步指导我吗?我希望方向保持在横向或根据屏幕方向反转横向。@suku,已更新。无论如何,我真的不明白你想要创建什么UI实现。嗨,Vyacheslav,你能不能请我根据你的答案对这个问题所做的编辑?你能进一步指导我吗?我希望方向保持在横向或根据屏幕方向反转横向。@suku,已更新。无论如何,我真的不明白你想要创建什么UI实现。
if (rotation == Surface.ROTATION_0 ){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if(rotation == Surface.ROTATION_90){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if (rotation == Surface.ROTATION_180 ){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else if(rotation == Surface.ROTATION_270){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}