本文主要是介绍android 指南针实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在onCreate 得到SensorManger实例
private SensorManager sensorManager = null;sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
onResume()要注册监听器
@Overrideprotected void onResume() {super.onResume();sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);}
onPause()要注销监听器
@Overrideprotected void onPause() {super.onPause();sensorManager.unregisterListener(this);}
实现监听返回结果的接口SensorEventListener
public class MainActivity extends Activity implements SensorEventListener {
}
@Overridepublic void onSensorChanged(SensorEvent event) {if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {// 获取绕Z轴转过的角度float tmp = event.values[0];Animation rotateAnimation = new RotateAnimation(angle, -tmp, directionTextView.getWidth() >> 1 ,directionTextView.getHeight() >> 1 );rotateAnimation.setDuration(100);rotateAnimation.setFillAfter(true);//动画之后就保持动画结束的位置directionTextView.startAnimation(rotateAnimation);angle =-tmp;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}
要旋转的图片
这篇关于android 指南针实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!