十 传感器
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准确度
---------------------------------------------------------------
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: 当传感器精度发生变化的时候 }
相关文章推荐
- 解决:java.sql.SQLException: 不能在 UTF8 和 UCS2 之间转换: failUTF8Conv
- location.hash的用法及onhashchange (精华,建议大家没事读一下)
- 魅族MX3 smart bar处失灵
- 20145101《Java程序设计》第10周学习总结
- 20150325--Redis-01
- 机器学习 —— 概率图模型(Homework: CRF Learning)
- 20145317彭垚 《Java程序设计》第五次实验报告
- java四种位运算
- 计算机网络的ip数据报首部各字段
- 80.JAVA编程思想——复杂性理论
- 使用 Log4j 的 NDC/MDC 改进日志
- 同余问题
- 27.连续子数组的最大和
- 20145105 《Java程序设计》第10周学习总结
- 20145317彭垚 《Java程序设计》第10周学习总结
- ISA TEST黑客过关小游戏第四关解密
- 一秒理解大端小端
- Swift 设置字体
- 79.JAVA编程思想——抽象应用
- PHP_CI框架(1)