在iOS上,设备方向可以与接口方向不同,因此我们有两个不同的枚举。我不一定要查找枚举。Android也有同样的区别吗?我想要检测接口方向的变化,而不是设备。
人们似乎在使用onConfigurationChanged()来检测方向的变化,但我并不清楚这到底是一种什么样的方向变化。
发布于 2016-10-27 02:47:55
这些方法可以检测(和)设置方向:
getResources().getConfiguration().orientation和setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
我正在使用这些方法来捕获所有方向:
onCreate内幕
initOrientation();
哪里
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) {
// 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))) {
//if ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && rotation != mLastRotation) {
Log.i(TAG, "changed >>> " + rotation + " :: " + _.mWidth);
// 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();
}
}这取决于你真正想要实现的东西。上述方法可以检测portrait>portrait和landscape>landscape移位。
为了强制锁定和释放旋转,我使用这种方法:
protected void mLockScreenRotation(int i)
{
// Stop the screen orientation changing during an event
switch (i)
{
default:
case 0:
switch (this.getResources().getConfiguration().orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Configuration.ORIENTATION_LANDSCAPE:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
case 1:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case 2:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}mLockScreenRotation(0) -锁定
https://stackoverflow.com/questions/40269810
复制相似问题