Android屏幕方向
就普通层面而言,大家理解的屏幕方向就是横竖屏(landscape和portrait。而随着Android平台的演进,对屏幕方向做出了更细致化的划分:可参见https://developer.android.com/guide/topics/manifest/activity-element.html#screen)。
传统获取方式1(不建议使用该方法。该方法使用前提,在清单文件里面没有固定Activity的方向)
覆写Activity的如下方法:(这里获取的就是横屏或者竖屏,没有更精细化划分)1
2
3
4
5
6
7
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Overall orientation of the screen. May be one of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT.
int or = newConfig.orientation;
}
获取方式2,根据WindowManager获取当前窗口的orientation(注意:适用于Android API level 8+)
1 | public String getRotation(Context context) { |
获取方式3,注册OrientationEventListener
在Activity内部处理方向事件较为简单。简单初始化一个OrientationEventListener并提供它的实现方法即可。比如, 参考如下代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35public class SimpleOrientationActivity extends Activity {
OrientationEventListener mOrientationListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOrientationListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_GAME) {
public void onOrientationChanged(int orientation) {
Log.v(DEBUG_TAG,
"Orientation changed to " + orientation);
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(DEBUG_TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(DEBUG_TAG, "Cannot detect orientation");
mOrientationListener.disable();
}
}
protected void onDestroy() {
super.onDestroy();
mOrientationListener.disable();
}
}
请注意上述代码中的new OrientationEventListener(this,SensorManager.SENSOR_DELAY_GAME)。这个构造方法的第二个参数,可以取几个值,这取决于应用的实际场景。The default rate, SENSOR_DELAY_NORMAL, 适合于简单的屏幕方向改变(suitable for screen orientation changes)。而SENSOR_DELAY_UI(suitable for the user interface),SENSOR_DELAY_GAME适合于游戏(suitable for games)。详细的取值模型请参考https://developer.android.com/reference/android/hardware/SensorManager.html#SENSOR_DELAY_NORMAL
After you have a valid OrientationEventListener object, you can check if it can detect orientation changes using the canDetectOrientation() method, and enable and disable the listener using its enable() and disable() methods.
The OrientationEventListener has a single callback method, which enables you to listen for orientation transitions, the onOrientationChanged() method. This method has a single parameter, an integer. This integer normally represents the device tilt as a number between 0 and 359:
- 未知方向:A result of ORIENTATION_UNKNOWN (-1) means the device is flat (perhaps on a table) and the orientation is unknown.
- 正常方向(对于普通手机而言,即portrait):A result of 0 means the device is in its “normal” orientation, with the top of the device facing in the up direction. (“Normal” is defined by the device manufacturer. You need to test on each device to find out for sure what “normal” means.)
- 逆向横屏(对于普通手机而言,即reverseLandscape):A result of 90 means the device is tilted 90 degrees, with the left side of the device facing in the up direction.
- 逆向竖屏(对于普通手机而言,即reversePortrait)A result of 180 means the device is tilted 180 degrees, with the bottom side of the device facing in the up direction (upside down).
- 横屏(对于普通手机而言,即landscape)A result of 270 means the device is tilted 270 degrees, with the right side of the device facing in the up direction.