Android 利用方向传感器实现 指南针

step1:新建一个项目Compass,并将一张指南针图片导入到res/drawable-hdpi目录中

1365944950_9473.jpg

step2:设计应用的UI界面,main.xml

 

[html] view plaincopy

  1. <span style=”font-size:18px;”><strong><?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:orientation=”vertical”
  4.     android:layout_width=”fill_parent”
  5.     android:layout_height=”fill_parent”
  6.     android:gravity=”center”
  7.     >
  8. <ImageView
  9.     android:layout_width=”wrap_content”
  10.     android:layout_height=”wrap_content”
  11.     android:src=”@drawable/compass”
  12.     android:id=”@+id/imageView”
  13.     />
  14. </LinearLayout></strong></span>


step3:MainActivity.java

 

 

[java] view plaincopy

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7. import android.os.Bundle;
  8. import android.view.animation.Animation;
  9. import android.view.animation.RotateAnimation;
  10. import android.widget.ImageView;
  11. public class MainActivity extends Activity {
  12.     private ImageView imageView;
  13.     /** 传感器管理器 */
  14.     private SensorManager manager;
  15.     private SensorListener listener = new SensorListener();
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.main);
  20.         imageView = (ImageView) this.findViewById(R.id.imageView);
  21.         imageView.setKeepScreenOn(true);//屏幕高亮
  22.         //获取系统服务(SENSOR_SERVICE)返回一个SensorManager 对象
  23.         manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  24.     }
  25.     @Override
  26.     protected void onResume() {
  27.         /**
  28.          *  获取方向传感器
  29.          *  通过SensorManager对象获取相应的Sensor类型的对象
  30.          */
  31.         Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  32.         //应用在前台时候注册监听器
  33.         manager.registerListener(listener, sensor,
  34.                 SensorManager.SENSOR_DELAY_GAME);
  35.         super.onResume();
  36.     }
  37.     @Override
  38.     protected void onPause() {
  39.         //应用不在前台时候销毁掉监听器
  40.         manager.unregisterListener(listener);
  41.         super.onPause();
  42.     }
  43.     private final class SensorListener implements SensorEventListener {
  44.         private float predegree = 0;
  45.         @Override
  46.         public void onSensorChanged(SensorEvent event) {
  47.             /**
  48.              *  values[0]: x-axis 方向加速度
  49.                values[1]: y-axis 方向加速度
  50.                values[2]: z-axis 方向加速度
  51.              */
  52.             float degree = event.values[0];// 存放了方向值
  53.             /**动画效果*/
  54.             RotateAnimation animation = new RotateAnimation(predegree, degree,
  55.                     Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  56.             animation.setDuration(200);
  57.             imageView.startAnimation(animation);
  58.             predegree=-degree;
  59.             /**
  60.             float x=event.values[SensorManager.DATA_X];
  61.             float y=event.values[SensorManager.DATA_Y];
  62.             float z=event.values[SensorManager.DATA_Z];
  63.             Log.i(“XYZ”, “x=”+(int)x+”,y=”+(int)y+”,z=”+(int)z);
  64.             */
  65.         }
  66.         @Override
  67.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  68.         }
  69.     }
  70. }

step4:AndroidManifest.xml

 

 

[html] view plaincopy

  1. <span style=”font-size:18px;”><strong><?xml version=”1.0″ encoding=”utf-8″?>
  2. <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
  3.       package=”cn.roco.sensor”
  4.       android:versionCode=”1″
  5.       android:versionName=”1.0″>
  6.     <uses-sdk android:minSdkVersion=”8″ />
  7.     <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
  8.         <activity android:name=”MainActivity”
  9.                   android:label=”@string/app_name”>
  10.             <intent-filter>
  11.                 <action android:name=”android.intent.action.MAIN” />
  12.                 <category android:name=”android.intent.category.LAUNCHER” />
  13.             </intent-filter>
  14.         </activity>
  15.     </application>
  16. </manifest></strong></span>

标签