zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android利用方向传感器实现指南针具体步骤

Android 实现 利用 方向 传感器 具体步骤 指南针
2023-06-13 09:15:00 时间
step1:新建一个项目Compass,并将一张指南针图片导入到res/drawable-hdpi目录中
 
step2:设计应用的UI界面,main.xml
复制代码代码如下:

<SPANstyle="FONT-SIZE:18px"><STRONG><?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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></STRONG></SPAN>

step3:MainActivity.java
复制代码代码如下:

importandroid.app.Activity;
importandroid.content.Context;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
importandroid.os.Bundle;
importandroid.view.animation.Animation;
importandroid.view.animation.RotateAnimation;
importandroid.widget.ImageView;
publicclassMainActivityextendsActivity{
privateImageViewimageView;
/**传感器管理器*/
privateSensorManagermanager;
privateSensorListenerlistener=newSensorListener();
@Override
publicvoidonCreate(BundlesavedInstanceState){
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);
}
@Override
protectedvoidonResume(){
/**
*获取方向传感器
*通过SensorManager对象获取相应的Sensor类型的对象
*/
Sensorsensor=manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
//应用在前台时候注册监听器
manager.registerListener(listener,sensor,
SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protectedvoidonPause(){
//应用不在前台时候销毁掉监听器
manager.unregisterListener(listener);
super.onPause();
}
privatefinalclassSensorListenerimplementsSensorEventListener{
privatefloatpredegree=0;
@Override
publicvoidonSensorChanged(SensorEventevent){
/**
*values[0]:x-axis方向加速度
  values[1]:y-axis方向加速度
  values[2]:z-axis方向加速度
*/
floatdegree=event.values[0];//存放了方向值
/**动画效果*/
RotateAnimationanimation=newRotateAnimation(predegree,degree,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(200);
imageView.startAnimation(animation);
predegree=-degree;

/**
floatx=event.values[SensorManager.DATA_X];
floaty=event.values[SensorManager.DATA_Y];
floatz=event.values[SensorManager.DATA_Z];
Log.i("XYZ","x="+(int)x+",y="+(int)y+",z="+(int)z);
*/
}
@Override
publicvoidonAccuracyChanged(Sensorsensor,intaccuracy){
}
}
}

step4:AndroidManifest.xml
复制代码代码如下:
<SPANstyle="FONT-SIZE:18px"><STRONG><?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="cn.roco.sensor"
android:versionCode="1"
android:versionName="1.0">
<uses-sdkandroid:minSdkVersion="8"/>
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
<activityandroid:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest></STRONG></SPAN>