我已经看到了一些与此类似但没有答案的其他问题.
从纵向旋转到横向(任一方向)再返回,我们得到对onConfigurationChanged()的有用调用.
但是,当从景观旋转到横向(通过180度)时,onConfigurationChanged()不会被调用.
我已经看到过使用OrientationEventListener,但这对我来说似乎很有趣,因为你可以快速旋转而不会触发显示方向的改变.
我已经尝试添加布局更改侦听器,但没有成功.
那么问题是,如何可靠地检测横向方向的这种变化?
sup*_*ser 10
当设备不旋转/移动时,OrientationEventlistener将不起作用.
我发现显示监听器是检测变化的更好方法.
DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {
android.util.Log.i(TAG, "Display #" + displayId + " added.");
}
@Override
public void onDisplayChanged(int displayId) {
android.util.Log.i(TAG, "Display #" + displayId + " changed.");
}
@Override
public void onDisplayRemoved(int displayId) {
android.util.Log.i(TAG, "Display #" + displayId + " removed.");
}
};
DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(mDisplayListener, UIThreadHandler);
Run Code Online (Sandbox Code Playgroud)
可能是你应该在OrientationEventListener中添加一些逻辑代码,如下所示:
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
OrientationEventListener orientationEventListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Display display = mWindowManager.getDefaultDisplay();
int rotation = display.getRotation();
if ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && rotation != mLastRotation) {
Log.i(TAG, "changed >>> " + rotation);
// do something
mLastRotation = rotation;
}
}
};
if (orientationEventListener.canDetectOrientation()) {
orientationEventListener.enable();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7496 次 |
| 最近记录: |