Android 电子罗盘--指南针(方向传感器的应用)

转载自:

http://blog.csdn.net/onlyonecoder/article/details/8475653

图片神马的在网上搜个指南针图片就好了,本来没有注释,发上来之后感觉不行,还是加上吧,方便学习



main.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:gravity="center"  
  7.     >  
  8.   
  9.     <ImageView  
  10.         android:id="@+id/compass_imageView"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:src="@drawable/compass" />  
  14.   
  15. LinearLayout>  


compass.Activity

[java]  view plain  copy
  1.   
[java]  view plain  copy
  1. import android.app.Activity;  
  2. import android.hardware.Sensor;  
  3. import android.hardware.SensorEvent;  
  4. import android.hardware.SensorEventListener;  
  5. import android.hardware.SensorManager;  
  6. import android.os.Bundle;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.RotateAnimation;  
  9. import android.widget.ImageView;  
  10.   
  11. /** 
  12.  * 电子罗盘 方向传感器 
  13.  */  
  14. public class ComPassActivity extends Activity implements SensorEventListener {  
  15.     private ImageView imageView;  
  16.     private float currentDegree = 0f;  
  17.   
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.compass);  
  21.         imageView = (ImageView) findViewById(R.id.compass_imageView);  
  22.   
  23.         // 传感器管理器  
  24.         SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);  
  25.         // 注册传感器(Sensor.TYPE_ORIENTATION(方向传感器);SENSOR_DELAY_FASTEST(0毫秒延迟);  
  26.         // SENSOR_DELAY_GAME(20,000毫秒延迟)、SENSOR_DELAY_UI(60,000毫秒延迟))  
  27.         sm.registerListener(ComPassActivity.this,  
  28.                 sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),  
  29.                 SensorManager.SENSOR_DELAY_FASTEST);  
  30.   
  31.     }  
  32.     //传感器报告新的值(方向改变)  
  33.     public void onSensorChanged(SensorEvent event) {  
  34.         if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {  
  35.             float degree = event.values[0];  
  36.               
  37.             /* 
  38.             RotateAnimation类:旋转变化动画类 
  39.               
  40.             参数说明: 
  41.  
  42.             fromDegrees:旋转的开始角度。 
  43.             toDegrees:旋转的结束角度。 
  44.             pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
  45.             pivotXValue:X坐标的伸缩值。 
  46.             pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
  47.             pivotYValue:Y坐标的伸缩值 
  48.             */  
  49.             RotateAnimation ra = new RotateAnimation(currentDegree, -degree,  
  50.                     Animation.RELATIVE_TO_SELF, 0.5f,  
  51.                     Animation.RELATIVE_TO_SELF, 0.5f);  
  52.             //旋转过程持续时间  
  53.             ra.setDuration(200);  
  54.             //罗盘图片使用旋转动画  
  55.             imageView.startAnimation(ra);  
  56.               
  57.             currentDegree = -degree;  
  58.         }  
  59.     }  
  60.     //传感器精度的改变  
  61.     public void onAccuracyChanged(Sensor sensor, int accuracy) {  
  62.   
  63.     }  
  64. }  

你可能感兴趣的:(Android,手表,4,android,应用,系统开发)