您的位置:首页 > 其它

十 传感器

2016-05-08 21:36 330 查看
一、注意事项:
1、有的手机并没有你写的这个传感器
2、 不同的系统版本引入的传感器是不同的
二、针对是否有传感功能优化
1、在运行时检测传感器
2、用Android Market过滤器来限定目标设备必须带有指定的传感器配置



三、使用注意事项

步骤:
1、得到传感器的服务
SensorManager .getSystemService(SENSOR_SERVCE)
2、得到具体的传感器
①Sensor .getXXXSensor(Sensor.TYPE_XXXXXXX)
②判断是否有这传感器
3、监听 接口 SensorEnvListener
(1)注册 onResume
(2)反注册 onPause
(3)重写
①传感器被改变 onSensorChanged
if RotateAnimation
②精度被改变 onAccuracyChanged

onSensorChanged
=传感器被改变时
=数据改变时
=判断(类型+执行对应操作)

动画:?

onAccuracyChanged准确度

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class SensorManagerActivity extends Activityimplements SensorEventListener {

private ImageView mImageView;
private SensorManager mSensorManager;
private Sensor mSensor;
private float mStartDegree = 0f;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_manager);

mImageView = (ImageView) findViewById(R.id.imageView);

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

}

@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
}

@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float degree = -event.values[0];

RotateAnimation rotateAnimation = new RotateAnimation(
mStartDegree, degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
);

rotateAnimation.setDuration(300);
mImageView.startAnimation(rotateAnimation);
mStartDegree = degree;
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

---------------------------------------------------------------

mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ALL);


@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float degree = -event.values[0];

RotateAnimation rotateAnimation = new RotateAnimation(
mStartDegree, degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
);

rotateAnimation.setDuration(300);
mImageView.startAnimation(rotateAnimation);
mStartDegree = degree;
} else 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];
if(x > 18 || y > 18 || z > 18 ){

}
}

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//TODO: 当传感器精度发生变化的时候
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: