OrientationWatchDog.java
4.35 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.wd.player.gesturedialog;
import android.content.Context;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;
/**
* 屏幕方向监听类
*
* @author lvjinhui
*/
public class OrientationWatchDog {
private static final String TAG = OrientationWatchDog.class.getSimpleName();
private Context mContext;
/**
* 系统的屏幕方向改变监听
*/
private OrientationEventListener mLandOrientationListener;
/**
* 对外的设置的监听
*/
private OnOrientationListener mOrientationListener;
/**
* 上次屏幕的方向
*/
private Orientation mLastOrientation = Orientation.Port;
/**
* 屏幕方向
*/
private enum Orientation {
/**
* 竖屏
*/
Port,
/**
* 横屏,正向
*/
Land_Forward,
/**
* 横屏,反向
*/
Land_Reverse;
}
public OrientationWatchDog(Context context) {
mContext = context.getApplicationContext();
}
/**
* 开始监听
*/
public void startWatch() {
if (mLandOrientationListener == null) {
mLandOrientationListener = new OrientationEventListener(mContext, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
if (orientation == -1) {
return;
}
// 这里的|| 和&& 不能弄错!!
// 根据手机的方向角度计算。在90和180度上下10度的时候认为横屏了。
// 竖屏类似。
boolean isLand =
(orientation < 100 && orientation > 80) || (orientation < 280 && orientation > 260);
boolean isPort =
(orientation < 10 || orientation > 350) || (orientation < 190 && orientation > 170);
if (isLand) {
if (mOrientationListener != null && (orientation < 100 && orientation > 80)) {
mOrientationListener.changedToLandReverseScape(
mLastOrientation == Orientation.Port || mLastOrientation == Orientation.Land_Forward);
mLastOrientation = Orientation.Land_Reverse;
} else if (mOrientationListener != null && (orientation < 280 && orientation > 260)) {
mOrientationListener.changedToLandForwardScape(
mLastOrientation == Orientation.Port || mLastOrientation == Orientation.Land_Reverse);
mLastOrientation = Orientation.Land_Forward;
}
} else if (isPort) {
if (mOrientationListener != null) {
mOrientationListener.changedToPortrait(mLastOrientation == Orientation.Land_Reverse
|| mLastOrientation == Orientation.Land_Forward);
}
mLastOrientation = Orientation.Port;
}
}
};
}
mLandOrientationListener.enable();
}
/**
* 结束监听
*/
public void stopWatch() {
if (mLandOrientationListener != null) {
mLandOrientationListener.disable();
}
}
/**
* 销毁监听
*/
public void destroy() {
stopWatch();
mLandOrientationListener = null;
}
/**
* 屏幕方向变化事件
*/
public interface OnOrientationListener {
/**
* 变为Land_Forward
*
* @param fromPort 是否是从竖屏变过来的
*/
void changedToLandForwardScape(boolean fromPort);
/**
* 变为Land_Reverse
*
* @param fromPort 是否是从竖屏变过来的
*/
void changedToLandReverseScape(boolean fromPort);
/**
* 变为Port
*
* @param fromLand 是否是从横屏变过来的
*/
void changedToPortrait(boolean fromLand);
}
/**
* 设置屏幕方向变化事件
*
* @param l 事件监听
*/
public void setOnOrientationListener(OnOrientationListener l) {
mOrientationListener = l;
}
}