Android——传感器的使用

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

下面显示了Sensor所提供的所有服务

Constants
int  TYPE_ACCELEROMETER   A constant describing an accelerometer sensor type. //三轴加速度感应器 返回三个坐标轴的加速度  单位m/s2
int  TYPE_ALL  A constant describing all sensor types.  //用于列出所有感应器
int  TYPE_GRAVITY  A constant describing a gravity sensor type.   //重力感应器
int  TYPE_GYROSCOPE  A constant describing a gyroscope sensor type //陀螺仪 可判断方向 返回三个坐标轴上的角度
int  TYPE_LIGHT  A constant describing an light sensor type.  //光线感应器 单位 lux 勒克斯
int  TYPE_LINEAR_ACCELERATION  A constant describing a linear acceleration sensor type. //线性加速度
int TYPE_MAGNETIC_FIELD  A constant describing a magnetic field sensor type.//磁场感应 返回三个
坐标轴的数值  微特斯拉
int TYPE_ORIENTATION  This constant is deprecated. use SensorManager.getOrientation() instead. //方向感应器 已过时 可以使用方法获得
int  TYPE_PRESSURE   A constant describing a pressure sensor type //压力感应器  单位 千帕斯卡
int TYPE_PROXIMITY   A constant describing an proximity sensor type.//距离传感器
int   TYPE_ROTATION_VECTOR   A constant describing a rotation vector sensor type.//翻转传感器
int   TYPE_TEMPERATURE  A constant describing a temperature sensor type //温度传感器 单位 摄氏度






指南针


public class MainActivity extends Activity {
private ImageView iv;
private SensorManager sensorManager;
private SensorEventListener listener;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView) findViewById(R.id.iv);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

}
protected void onResume() {

//得到传感器
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
listener = new MyListener();
//传感器接口 ,传感器, 传感器频率
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
super.onRestart();
}

private class MyListener implements SensorEventListener {
private float predegree = 0;
//传感器发生变换的时候
@Override
public void onSensorChanged(SensorEvent event) {

float degree = event.values[0];// 存放了方向值
/**动画效果*/
RotateAnimation animation = new RotateAnimation(predegree, degree,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(200);
iv.startAnimation(animation);
predegree=-degree;

/**
float x=event.values[SensorManager.DATA_X];
float y=event.values[SensorManager.DATA_Y];
float z=event.values[SensorManager.DATA_Z];
Log.i("XYZ", "x="+(int)x+",y="+(int)y+",z="+(int)z);
*/
}


@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}


}
@Override
protected void onDestroy() {
//应用不在前台时候销毁掉监听器
sensorManager.unregisterListener(listener);
listener=null;
super.onDestroy();
}
}

猜你喜欢

转载自blog.csdn.net/qq524752841/article/details/42421667