package com.vastweb.securitypatrolmvp.util;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import com.tencent.tencentmap.mapsdk.maps.LocationSource;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
/**
* @author : Life
* onCreate DateTime 2021/3/23 : 10:31
* in order to : 辅助定位图标准确的显示指向;利用手机传感器
**/
public class SensorEventHelper implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private long lastTime = 0;
private final int TIME_SENSOR = 100;
private float mAngle;
private Context mContext;
private Marker mMarker;
private LocationSource.OnLocationChangedListener mChangedListener;
private Location mLocation;
public SensorEventHelper(Context context) {
mContext = context;
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
LogUtil.info(this.getClass().getName() + "进入传感器控制");
}
public void registerSensorListener() {
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public void unRegisterSensorListener() {
mSensorManager.unregisterListener(this, mSensor);
}
public void setCurrentMarker(Marker marker) {
mMarker = marker;
}
public void setLocationSource(Location location) {
mLocation = location;
}
public void setLocationListener(LocationSource.OnLocationChangedListener listener) {
mChangedListener = listener;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if (System.currentTimeMillis() - lastTime < TIME_SENSOR) {
return;
}
LogUtil.info(this.getClass().getName() + "传感反馈");
switch (event.sensor.getType()) {
case Sensor.TYPE_ORIENTATION: {
LogUtil.info(this.getClass().getName() + "传感反馈:方向类型");
float x = event.values[0];
x += getScreenRotationOnPhone(mContext);
x %= 360.0F;
if (x > 180.0F) x -= 360.0F;
else if (x < -180.0F) x += 360.0F;
if (Math.abs(mAngle - x) < 3.0f) {
break;
}
mAngle = Float.isNaN(x) ? 0 : x;
if (mMarker != null) {
mMarker.setRotation(360 - mAngle);
}
LogUtil.info(this.getClass().getName() + "传感反馈:方向类型--" + (mChangedListener != null) + ":" + (mLocation != null));
if (mChangedListener != null && mLocation != null) {
mLocation.setBearing(mAngle);
mChangedListener.onLocationChanged(mLocation);
}
lastTime = System.currentTimeMillis();
}
}
}
/**
* 获取当前屏幕旋转角度
*
* @param context
* @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏
*/
public static int getScreenRotationOnPhone(Context context) {
final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return -90;
}
return 0;
}
}
代码从网络复制整理,如有侵权,联系立马删除!