一、使用OrientationEventListener监测指定的屏幕旋转角度,继承OrientationEventListener类监听手机的旋转角度的变化。
二、直接贴源码吧?MainActivity.java
package com.example.sensordeom;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
//1,获得SensorManager对象
//2,获得想要的Sensor对象
//3,绑定监听器
public class MainActivity extends Activity {
public static final String TAG = "SENSORDEMO";
Button findBut,accelerationBut,lightBut,orientationBut,proximityBut;
SensorManager sensorManager;
TextView text,accText,luxText;
float gravity[]=new float[3];
float linear_acceleration[]=new float[3];
private TextView oriBtn;
private MyOrientoinListener myOrientoinListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oriBtn = (TextView) findViewById(R.id.orientation);
myOrientoinListener = new MyOrientoinListener(this);
myOrientoinListener.enable();
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁时取消监听
myOrientoinListener.disable();
}
class MyOrientoinListener extends OrientationEventListener {
public MyOrientoinListener(Context context) {
super(context);
}
public MyOrientoinListener(Context context, int rate) {
super(context, rate);
}
@Override
public void onOrientationChanged(int orientation) {
String hint="";
int angle=0;
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
hint="手机平放"; //return; //手机平放时,检测不到有效的角度
}
//可以根据不同角度检测处理,这里只检测四个角度的改变
if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) { //0度
angle = 0;
} else if (orientation > 45 && orientation < 135) { //180度
angle = 90;
} else if (orientation > 135 && orientation < 225) { //270度
angle = 180;
} else if (orientation > 225 && orientation < 315) { //90度
angle = 270;
}
oriBtn.setText(hint+" orientation="+orientation+" angle="+angle);
}
}
}
三、app测试效果
?
?
?四,分析framework下的主要实现这个功能的源码,详细代码请看frameworks\base\core\java\android\view\OrientationEventListener.java,这里只贴出核心的代码SensorEventListenerImpl,这里会向app发出设备旋转的变动信息。
class SensorEventListenerImpl implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int orientation = ORIENTATION_UNKNOWN;
float X = -values[_DATA_X];
float Y = -values[_DATA_Y];
float Z = -values[_DATA_Z];
float magnitude = X*X + Y*Y;
// Don't trust the angle if the magnitude is small compared to the y value
if (magnitude * 4 >= Z*Z) {
float OneEightyOverPi = 57.29577957855f;
float angle = (float)Math.atan2(-Y, X) * OneEightyOverPi;
orientation = 90 - (int)Math.round(angle);
// normalize to 0 - 359 range
while (orientation >= 360) {
orientation -= 360;
}
while (orientation < 0) {
orientation += 360;
}
}
if (mOldListener != null) {
mOldListener.onSensorChanged(Sensor.TYPE_ACCELEROMETER, event.values);
}
if (orientation != mOrientation) {
mOrientation = orientation;
onOrientationChanged(orientation);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
?五、参考文章。
https://www.freesion.com/article/9784538600/
https://blog.csdn.net/u010029439/article/details/100106687
?https://blog.csdn.net/youmingyu/article/details/52750374
https://www.jb51.net/article/185034.htm
|