我有一个应用程序,在一个活动中显示全屏位图。为了提供快速加载时间,我在内存中加载它们。但是当屏幕方向改变时,我想清除缓存,以便再次填充适合新尺寸的位图。唯一的问题是,为了做到这一点,我需要检测何时发生方向改变。有人知道如何检测吗?
我有一个应用程序,在一个活动中显示全屏位图。为了提供快速加载时间,我在内存中加载它们。但是当屏幕方向改变时,我想清除缓存,以便再次填充适合新尺寸的位图。唯一的问题是,为了做到这一点,我需要检测何时发生方向改变。有人知道如何检测吗?
请查看官方文档http://developer.android.com/guide/topics/resources/runtime-changes.html
更改它将实际创建一个新视图,并再次调用onCreate。
此外,您可以通过
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
onCreate
方法中检查onSavedInstanceState
,如果它不为null,则表示这是配置更改。 OrientationEventListener mOrientationEventListener = new OrientationEventListener(
this, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
//checking if device was rotated
if (orientationPortrait != isPortrait(orientation)) {
orientationPortrait = !orientationPortrait;
Log.d(TAG, "Device was rotated!");
}
}
};
检查设备方向:
private boolean isPortrait(int orientation) {
return (orientation >= (360 - 90) && orientation <= 360) || (orientation >= 0 && orientation <= 90);
}
不要忘记启用和禁用监听器:
if (mOrientationEventListener != null) {
mOrientationEventListener.enable();
}
if (mOrientationEventListener != null) {
mOrientationEventListener.disable();
}
OnCreate()
方法,除非你已经做了一些特殊处理。你可以在这里添加逻辑代码。onSavedInstanceState
,还有https://dev59.com/Z2855IYBdhLWcg3w5Igb - sealz