Pro Android学习笔记(一五七):传感器(7):其他一些传感器

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei

Wei:本来想下面接着的是NFC,NFC是支持读写的,和其他传感器在编程上很不一样,但是手头上找不到支持NFC的手机,记得以前三星Galaxy系列支持,例如几年前的S3,而国产手机要比较贵的才支持,P6和Z7 mini都不支持,有兴趣可以阅读http://blog.csdn.net/pku_android/article/details/7430788

重力传感器(Gravity Sensors)

重力传感器不是物理传感器,是在加速度传感器的基础上,结合其他的硬件,例如陀螺仪,获得更精确的数据。其读数含义同加速度传感器。

线性加速度传感器(Linear Acceleration Sensors)

线性加速度传感器也不是物理传感器,是在加速度传感器的基础上,结合其他的硬件,例如陀螺仪,获得更精确的数据。线性加速度传感器的数值是加速度减去重力。对于静止的物体,x,y,z为均为接近0的很小值。

旋转矢量传感器(Rotation Vector Sensors)

通过旋转矢量传感器将获得四元数(quaternion),即[w,x,y,z],得到设备的空间摆放情况,伟大的数学,不认识。大抵看用于3D游戏等,在此只做读取。利用旋转矢量传感器也可以获得设备的位置。下面是相关的代码:

public class VirtualJax extends Activity implements SensorEventListener{
    ....     
    private float[] rotVecValues = null;
    private float[] rotvecR = new float[9],rotQ = new float[4];
    private float[] rotvecOrientValues = new float[3];
   
    @Override
    public void onSensorChanged(SensorEvent event) {
        ... ...
        if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR){       
            if(rotVecValues == null){
                rotVecValues = new float[event.values.length];
            }
            for(int i = 0; i < rotVecValues.length; i++){
                rotVecValues[i] = event.values[i];
            }
   
            if(rotVecValues != null){
                SensorManager.getQuaternionFromVector(rotQ, rotVecValues);
                SensorManager.getRotationMatrixFromVector(rotvecR, rotVecValues);
                SensorManager.getOrientation(rotvecR, rotvecOrientValues);
            }
        }            
    }
   
    public void doUpdate(View v){
        ......
        if(rotvecOrientValues != null && mRotation == Surface.ROTATION_0){
            msg += String.format("Rotation Vector Sensor:\nazimuth %7.3f\npitch %7.3f\nroll %7.3f\nw,x,y,z %6.2f,%6.2f,%6.2f,%6.2f\n",
                    Math.toDegrees(rotvecOrientValues[0]),
                    Math.toDegrees(rotvecOrientValues[1]),
                    Math.toDegrees(rotvecOrientValues[2]),
                    rotQ[0],rotQ[1],rotQ[2],rotQ[3]);
        }   
        nowOne.setText(msg);
        ......
    }
   
}

运行情况如下:

相关小例子代码:Pro Android学习:传感器小例子

相关链接:我的Android开发相关文章

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值