标签:
1 public class SensorZhinan extends Activity implements SensorEventListener{ 2 3 private ImageView point; 4 private SensorManager sensor; 5 private float currentDegree=0; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 // TODO Auto-generated method stub 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_zhinanzheng); 11 point=(ImageView) findViewById(R.id.point_image); 12 sensor=(SensorManager) getSystemService(Context.SENSOR_SERVICE); 13 14 } 15 16 @Override 17 protected void onResume() { 18 // TODO Auto-generated method stub 19 super.onResume();
//注册方向传感器 20 sensor.registerListener(this, sensor.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME); 21 22 } 23 24 @Override 25 protected void onPause() { 26 // TODO Auto-generated method stub 27 super.onPause(); 28 sensor.unregisterListener(this); 29 } 30 @Override 31 protected void onStop() { 32 // TODO Auto-generated method stub 33 super.onStop(); 34 sensor.unregisterListener(this); 35 } 36 37 38 @Override 39 public void onSensorChanged(SensorEvent event) { 40 // TODO Auto-generated method stub 41 float degree=event.values[0]; 42 43 RotateAnimation ra=new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 44 ra.setDuration(200); 45 point.startAnimation(ra); 46 currentDegree=-degree; 47 } 48 @Override 49 public void onAccuracyChanged(Sensor sensor, int accuracy) { 50 // TODO Auto-generated method stub 51 52 } 53 54 @Override 55 protected void onDestroy() { 56 // TODO Auto-generated method stub 57 super.onDestroy(); 58 sensor.unregisterListener(this); 59 finish(); 60 } 61 }
标签:
原文地址:http://www.cnblogs.com/ware/p/4432319.html