Android 利用方向传感器实现指南针具体步骤
短信预约 -IT技能 免费直播动态提醒
step1:新建一个项目Compass,并将一张指南针图片导入到res/drawable-hdpi目录中
step2:设计应用的UI界面,main.xml
代码如下:
<SPAN style="FONT-SIZE: 18px"><STRONG><?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:class="lazy" data-src="@drawable/compass"
android:id="@+id/imageView"
/>
</LinearLayout></STRONG></SPAN>
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();
@Override
public 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);
}
@Override
protected void onResume() {
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
//应用在前台时候注册监听器
manager.registerListener(listener, sensor,
SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause() {
//应用不在前台时候销毁掉监听器
manager.unregisterListener(listener);
super.onPause();
}
private final class SensorListener implements SensorEventListener {
private float predegree = 0;
@Override
public void onSensorChanged(SensorEvent event) {
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;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
}
step4:AndroidManifest.xml
代码如下:
<SPAN style="FONT-SIZE: 18px"><STRONG><?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></STRONG></SPAN>
您可能感兴趣的文章:Android编程之光线传感器用法详解Android编程使用光线传感器获取光线强弱的方法【LightSensorManager封装类】Android开发中方向传感器定义与用法详解【附指南针实现方法】Android开发中的重力传感器用法实例详解Android编程之方向传感器用法示例Android编程中光线传感器的调用方法详解android 传感器(OnSensorChanged)使用介绍Android利用方向传感器获得手机的相对角度实例说明Android 传感器--光照传感器详解及使用Android重力传感器实现滚动的弹球Android操作系统介绍之11种传感器Android编程基于距离传感器控制手机屏幕熄灭的方法详解
step2:设计应用的UI界面,main.xml
代码如下:
<SPAN style="FONT-SIZE: 18px"><STRONG><?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:class="lazy" data-src="@drawable/compass"
android:id="@+id/imageView"
/>
</LinearLayout></STRONG></SPAN>
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();
@Override
public 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);
}
@Override
protected void onResume() {
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
//应用在前台时候注册监听器
manager.registerListener(listener, sensor,
SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause() {
//应用不在前台时候销毁掉监听器
manager.unregisterListener(listener);
super.onPause();
}
private final class SensorListener implements SensorEventListener {
private float predegree = 0;
@Override
public void onSensorChanged(SensorEvent event) {
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;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
}
step4:AndroidManifest.xml
代码如下:
<SPAN style="FONT-SIZE: 18px"><STRONG><?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></STRONG></SPAN>
您可能感兴趣的文章:Android编程之光线传感器用法详解Android编程使用光线传感器获取光线强弱的方法【LightSensorManager封装类】Android开发中方向传感器定义与用法详解【附指南针实现方法】Android开发中的重力传感器用法实例详解Android编程之方向传感器用法示例Android编程中光线传感器的调用方法详解android 传感器(OnSensorChanged)使用介绍Android利用方向传感器获得手机的相对角度实例说明Android 传感器--光照传感器详解及使用Android重力传感器实现滚动的弹球Android操作系统介绍之11种传感器Android编程基于距离传感器控制手机屏幕熄灭的方法详解
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341