-
Notifications
You must be signed in to change notification settings - Fork 34
/
OrientationUtil.java
129 lines (115 loc) · 3.45 KB
/
OrientationUtil.java
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
package com.liyi.sutils.utils;
import android.content.Context;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;
/**
* 屏幕方向监听工具类
*/
public class OrientationUtil {
/* 方向事件监听器 */
private OrientationEventListener mOrientationEventListener;
/* 监测屏幕方向的监听器 */
private OnOrientationListener mOrientationListener;
/* 传感器等级 */
private int mRate;
private Context mContext;
private OrientationUtil() {
this.mContext = SUtils.getApp().getApplicationContext();
this.mRate = SensorManager.SENSOR_DELAY_NORMAL;
}
public static OrientationUtil getInstance() {
return OrientationUtilHolder.INSTANCE;
}
private static final class OrientationUtilHolder {
private static final OrientationUtil INSTANCE = new OrientationUtil();
}
/**
* 初始化
*/
private void init() {
mOrientationEventListener = new OrientationEventListener(mContext, mRate) {
@Override
public void onOrientationChanged(int orientation) {
if (mOrientationListener != null) {
mOrientationListener.orientationChanged(orientation);
}
}
};
}
/**
* 设置传感器的等级(默认为:SensorManager.SENSOR_DELAY_NORMAL)
*
* @param rate 传感器的等级
* <ul>
* <li>{@link SensorManager#SENSOR_DELAY_FASTEST}: 0</li>
* <li>{@link SensorManager#SENSOR_DELAY_GAME}: 1</li>
* <li>{@link SensorManager#SENSOR_DELAY_UI}: 2</li>
* <li>{@link SensorManager#SENSOR_DELAY_NORMAL}: 3</li>
* </ul>
* @return {@link OrientationUtil}
*/
public OrientationUtil rate(int rate) {
this.mRate = rate;
return this;
}
/**
* 设置屏幕方向监听方法
*
* @param listener
* @return {@link OrientationUtil}
*/
public OrientationUtil callback(OnOrientationListener listener) {
this.mOrientationListener = listener;
return this;
}
/**
* 开启屏幕方向监测
*/
public void start() {
if (mOrientationEventListener == null) {
init();
}
if (isEnabled()) {
mOrientationEventListener.enable();
} else {
mOrientationEventListener.disable();
}
}
/**
* 停止屏幕方向监测
*/
public void stop() {
if (mOrientationEventListener != null) {
mOrientationEventListener.disable();
}
mOrientationListener = null;
}
/**
* 判断屏幕方向监听是否可用
*
* @return {@code true}: 可用<br>{@code false}: 不可用
*/
public boolean isEnabled() {
if (mOrientationEventListener == null) return false;
return mOrientationEventListener.canDetectOrientation();
}
/**
* 获取方向事件监听器
*
* @return orientationEventListener
*/
public OrientationEventListener getOrientationEventListener() {
return mOrientationEventListener;
}
/**
* 监测屏幕方向的监听器
*/
public interface OnOrientationListener {
/**
* 监测屏幕方向监听
*
* @param orientation 屏幕角度(范围:0 - 360)
*/
void orientationChanged(int orientation);
}
}