Android开发中的重力传感器用法实例详解
作者:东风破 时间:2022-01-28 12:57:01
本文实例讲述了Android开发中的重力传感器用法。分享给大家供大家参考,具体如下:
重力传感器与方向传感器的开发步骤类似,只要理清了期中的x,y,z的值之后就可以根据他们的变化来进行编程了,首先来看一副图
假设当地的重力加速度值为g
当手机正面朝上的时候,z的值为q,反面朝上的时候,z的值为-g
当手机右侧面朝上的时候,x的值为g,右侧面朝上的时候,x的值为-g
当手机上侧面朝上的时候,y的值为g,右侧面朝上的时候,y的值为-g
了解了重力传感器中X,Y,Z的含义之后下面我们就开始学习如何使用
首先我们创建一个传感器管理器和一个传感器 * ,管理器用来管理传感器以及创建各种各样的传感器, * 用来监视传感器的变化并且进行相应的操作
private SensorManager sensorManager;
private MySensorEventListener mySensorEventListener;
mySensorEventListener= new MySensorEventListener();//这个 * 当然是我们自己定义的,在重力感应器感应到手机位置有变化的时候,我们可以采取相应的操作,这里紧紧是将x,y,z的值打印出来
private final class MySensorEventListener implements SensorEventListener{
@Override
//可以得到传感器实时测量出来的变化值
public void onSensorChanged(SensorEvent event) {
//重力传感器
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
//tv_accelerometer是界面上的一个TextView标签,不再赘述
tv_orientation.setText("Orientation:"+x+","+y+","+z);
}
}
我们在onResume()
方法中创建重力传感器,并向系统注册 *
protected void onResume() {
Sensor sensor_accelerometer=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(mySensorEventListener,sensor_accelerometer, SensorManager.SENSOR_DELAY_UI);
super.onResume();
}
最后我们在onPause()
中注销所有传感器的监听,释放重力感应器资源!
protected void onPause() {
//注销所有传感器的监听
sensorManager.unregisterListener(mySensorEventListener);
super.onPause();
}
到此,有关重力传感器的介绍完毕!
接下来看一个Android用重力传感器做横竖屏切换的例子
在播放视频的时候,可能要做横竖屏的切换,但是,用户可以设置自己的手机关掉屏幕旋转,这个时候就需要想其他的办法了,比如:重力传感器。
public class ScreenSwitchUtils {
private static final String TAG = ScreenSwitchUtils.class.getSimpleName();
private volatile static ScreenSwitchUtils mInstance;
private Activity mActivity;
// 是否是竖屏
private boolean isPortrait = true;
private SensorManager sm;
private OrientationSensorListener listener;
private Sensor sensor;
private SensorManager sm1;
private Sensor sensor1;
private OrientationSensorListener1 listener1;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 888:
int orientation = msg.arg1;
if (orientation > 45 && orientation < 135) {
} else if (orientation > 135 && orientation < 225) {
} else if (orientation > 225 && orientation < 315) {
if (isPortrait) {
Log.e(test, 切换成横屏);
mActivity.setRequestedOrientation(0);
isPortrait = false;
}
} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {
if (!isPortrait) {
Log.e(test,切换成竖屏);
mActivity.setRequestedOrientation(1);
isPortrait = true;
}
}
break;
default:
break;
}
};
};
/** 返回ScreenSwitchUtils单例 **/
public static ScreenSwitchUtils init(Context context) {
if (mInstance == null) {
synchronized (ScreenSwitchUtils.class) {
if (mInstance == null) {
mInstance = new ScreenSwitchUtils(context);
}
}
}
return mInstance;
}
private ScreenSwitchUtils(Context context) {
Log.d(TAG, init orientation listener.);
// 注册重力感应器,监听屏幕旋转
sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener = new OrientationSensorListener(mHandler);
// 根据 旋转之后/点击全屏之后 两者方向一致,激活sm.
sm1 = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor1 = sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener1 = new OrientationSensorListener1();
}
/** 开始监听 */
public void start(Activity activity) {
Log.d(TAG, start orientation listener.);
mActivity = activity;
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
}
/** 停止监听 */
public void stop() {
Log.d(TAG, stop orientation listener.);
sm.unregisterListener(listener);
sm1.unregisterListener(listener1);
}
/**
* 手动横竖屏切换方向
*/
public void toggleScreen() {
sm.unregisterListener(listener);
sm1.registerListener(listener1, sensor1,SensorManager.SENSOR_DELAY_UI);
if (isPortrait) {
isPortrait = false;
// 切换成横屏
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
isPortrait = true;
// 切换成竖屏
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
public boolean isPortrait(){
return this.isPortrait;
}
/**
* 重力感应监听者
*/
public class OrientationSensorListener implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public static final int ORIENTATION_UNKNOWN = -1;
private Handler rotateHandler;
public OrientationSensorListener(Handler handler) {
rotateHandler = handler;
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
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 (rotateHandler != null) {
rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();
}
}
}
public class OrientationSensorListener1 implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public static final int ORIENTATION_UNKNOWN = -1;
public OrientationSensorListener1() {
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
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 (orientation > 225 && orientation < 315) {// 检测到当前实际是横屏
if (!isPortrait) {
sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);
sm1.unregisterListener(listener1);
}
} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {// 检测到当前实际是竖屏
if (isPortrait) {
sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);
sm1.unregisterListener(listener1);
}
}
}
}
}
使用的时候:
public class MainActivity extends Activity implements OnClickListener {
private ScreenSwitchUtils instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = ScreenSwitchUtils.init(this.getApplicationContext());
}
@Override
protected void onStart() {
super.onStart();
instance.start(this);
}
@Override
protected void onStop() {
super.onStop();
instance.stop();
}
@SuppressLint(NewApi)
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e(test, onConfigurationChanged);
if (instance.isPortrait()) {
// 切换成竖屏
LayoutParams params1 = new RelativeLayout.LayoutParams(screenWidth, DensityUtil.dip2px(this, 160));
videoView.setLayoutParams(params1);
Toast.makeText(getApplicationContext(), 竖屏, 0).show();
Log.e(test, 竖屏);
} else {
// 切换成横屏
LayoutParams params1 = new RelativeLayout.LayoutParams(screenHeight, screenWidth);
videoView.setLayoutParams(params1);
Toast.makeText(getApplicationContext(), 横屏, 0).show();
Log.e(test, 横屏);
}
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.iv_stretch:
instance.toggleScreen();
break;
}
}
}
调用了activity.setRequestedOrientation()
以后,会触发activity.onConfigurationChanged();
可以在这里面重新设置播放界面的大小。
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.sina.com.cn/s/blog_5a48dd2d0100u5ej.html
标签:Android,重力传感器
0
投稿
猜你喜欢
C#调用JS的几种方法
2022-09-29 23:24:18
Android中RecyclerView实现分页滚动的方法详解
2023-11-13 13:59:53
Java Switch对各类型支持实现原理
2023-10-28 04:45:37
SpringBoot项目集成Swagger和swagger-bootstrap-ui及常用注解解读
2023-03-17 06:30:20
Java单例模式利用HashMap实现缓存数据
2021-12-15 20:21:42
C#中abstract的用法详解
2021-07-08 21:53:46
c语言重要的字符串与内存函数
2023-04-28 00:35:42
Java反射及性能详细
2023-08-20 20:49:42
Flutter 分页功能表格控件详细解析
2023-09-22 20:02:45
C#算法之无重复字符的最长子串
2021-05-24 21:56:59
C# WinForm编程获取文件物理路径的方法
2021-08-30 16:06:06
Mybatis如何实现@Select等注解动态组合SQL语句
2022-04-13 16:42:05
C#检查指定对象是否存在于ArrayList集合中的方法
2023-07-28 00:45:17
JAVA线程池原理实例详解
2021-10-01 17:59:01
WPF实现文字粒子闪烁动画效果
2022-01-12 23:56:45
Android中BroadcastReceiver(异步接收广播Intent)的使用
2022-12-24 20:51:17
Servlet中/和/*的区别详解
2022-07-11 03:21:33
一文带你搞懂Java中的递归
2022-10-08 07:34:04
一文搞懂Java顶层类之Object类的使用
2023-06-02 06:15:42
Spring Security 中如何让上级拥有下级的所有权限(案例分析)
2022-01-28 16:55:26