Android开发获取重力加速度和磁场强度的方法
程序员文章站
2023-12-11 20:35:35
本文实例讲述了android开发获取重力加速度和磁场强度的方法。分享给大家供大家参考,具体如下:
android获取重力加速度和磁场强度主要依靠:
sensor...
本文实例讲述了android开发获取重力加速度和磁场强度的方法。分享给大家供大家参考,具体如下:
android获取重力加速度和磁场强度主要依靠:
sensor.getrotationmatrix (float[] r, float[] i, float[] gravity, float[] geomagnetic)
输入数据:
gravity为重力传感器测得的重力加速度float[3];
geomagnetic为罗盘传感器测得的地磁数据float[3];
输出数据:
r为通过这个方法计算得到的从手机的设备到坐标系到真实世界坐标系的转换矩阵
i为从将地磁数据转换到重力数据所在坐标的旋转矩阵
[0 0 g] = r * gravity (g = 重力加速度)
[0 m 0] = i * r * geomagnetic (m = 磁场强度)
下面是我做的结果图
核心部分代码如下:
public class mainactivity extends appcompatactivity implements sensoreventlistener { private static final string tag = "compassactivity"; private sensormanager sensormanager; //记录rotationmatrix矩阵值 private float[] r = new float[9]; private float[] gravity = null; private float[] geomagnetic = null; private float[] i=new float[9]; private handler handler=new handler(){ @override public void handlemessage(message msg) { if(gravity!=null&&geomagnetic!=null) { if(sensormanager.getrotationmatrix(r, i, gravity, geomagnetic)) { float gri = gravity[0] * r[6] + gravity[1] * r[7] + gravity[2] * r[8]; float h= (i[3]*r[0]+i[4]*r[3]+i[5]*r[6])*geomagnetic[0]+ (i[3]*r[1]+i[4]*r[4]+i[5]*r[7])*geomagnetic[1]+ (i[3]*r[2]+i[4]*r[5]+i[5]*r[8])*geomagnetic[2]; textview textview = (textview) findviewbyid(r.id.textview); textview.settext("重力加速度" + gri + "\n"+"磁场强度" + h + "\n"); } } } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sensormanager = (sensormanager) getsystemservice(sensor_service); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. the action bar will // automatically handle clicks on the home/up button, so long // as you specify a parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onsensorchanged(sensorevent sensorevent) { switch (sensorevent.sensor.gettype()){ case sensor.type_accelerometer: gravity=sensorevent.values; handler.sendemptymessage(0); break; case sensor.type_magnetic_field: geomagnetic=sensorevent.values; handler.sendemptymessage(0); break; default:break; } } @override public void onaccuracychanged(sensor sensor, int i) { } private void showmessage(string message){ view view=findviewbyid(r.id.fab); final snackbar snackbar=snackbar.make(view, message, snackbar.length_indefinite); snackbar.setaction("关闭", new view.onclicklistener() { @override public void onclick(view view) { snackbar.dismiss(); } }); snackbar.show(); } @override protected void onpause() { super.onpause(); sensormanager.unregisterlistener(this); } @override protected void onresume() { super.onresume(); sensor sensor1=sensormanager.getdefaultsensor(sensor.type_accelerometer); sensormanager.registerlistener(this,sensor1,sensormanager.sensor_delay_ui); sensor sensor2=sensormanager.getdefaultsensor(sensor.type_magnetic_field); sensormanager.registerlistener(this,sensor2,sensormanager.sensor_delay_ui); } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android文件操作技巧汇总》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。