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


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

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/compass" android:id="@+id/imageView" /> </LinearLayout>

step3:MainActivity.java

import android.app.Activity;import android.content.Context;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 MainActivity extends Activity {private ImageView imageView;/** 传感器管理器 */private SensorManager manager;private SensorListener listener = new SensorListener();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);imageView = (ImageView) this.findViewById(R.id.imageView);imageView.setKeepScreenOn(true);//屏幕高亮//获取系统服务(SENSOR_SERVICE)返回一个SensorManager 对象manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);}@Overrideprotected void onResume() {/** *  获取方向传感器 *  通过SensorManager对象获取相应的Sensor类型的对象 */Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);//应用在前台时候注册监听器manager.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_GAME);super.onResume();}@Overrideprotected void onPause() {//应用不在前台时候销毁掉监听器manager.unregisterListener(listener);super.onPause();}private final class SensorListener implements SensorEventListener {private float predegree = 0;@Overridepublic void onSensorChanged(SensorEvent event) {/** *  values[0]: x-axis 方向加速度   values[1]: y-axis 方向加速度   values[2]: z-axis 方向加速度 */float degree = event.values[0];// 存放了方向值/**动画效果*/RotateAnimation animation = new RotateAnimation(predegree, degree,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);animation.setDuration(200);imageView.startAnimation(animation);predegree=-degree;/**float x=event.values[SensorManager.DATA_X];float y=event.values[SensorManager.DATA_Y];float z=event.values[SensorManager.DATA_Z];Log.i("XYZ", "x="+(int)x+",y="+(int)y+",z="+(int)z);*/}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}}}

step4:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.roco.sensor" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>


更多相关文章

  1. 下拉菜单中自动亮度调节&P 上背光调节
  2. Android中传感器的一些功能
  3. Android界面导航之拖放框架(Drag and Drop)
  4. 跟我学android之四 事件驱动模型
  5. Android Activity之---(生命周期)
  6. [置顶] Android 界面滑动实现---Scroller类 从源码和开发文档中

随机推荐

  1. Android调用系统发送短信界面
  2. item嵌套button使listview onitemClick事
  3. android 暗水印
  4. android源码解析-异步消息
  5. android 4.4以下loadIocn nullpointerExc
  6. eclipse中在线升级adt遇到问题
  7. Android Studio 上如何使用LogCat
  8. android之通过MediaRecorder进行手机录音
  9. Android手机的USB
  10. Android 广播获取短信内容