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

Android 电子罗盘 --指南针(方向传感器的应用)_第1张图片


main.xml

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


compass.Activity

  
import android.app.Activity;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 ComPassActivity extends Activity implements SensorEventListener {private ImageView imageView;private float currentDegree = 0f;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.compass);imageView = (ImageView) findViewById(R.id.compass_imageView);// 传感器管理器SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);// 注册传感器(Sensor.TYPE_ORIENTATION(方向传感器);SENSOR_DELAY_FASTEST(0毫秒延迟);// SENSOR_DELAY_GAME(20,000毫秒延迟)、SENSOR_DELAY_UI(60,000毫秒延迟))sm.registerListener(ComPassActivity.this,sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);}//传感器报告新的值(方向改变)public void onSensorChanged(SensorEvent event) {if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {float degree = event.values[0];/*RotateAnimation类:旋转变化动画类 参数说明:fromDegrees:旋转的开始角度。toDegrees:旋转的结束角度。pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。pivotXValue:X坐标的伸缩值。pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。pivotYValue:Y坐标的伸缩值*/RotateAnimation ra = new RotateAnimation(currentDegree, -degree,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);//旋转过程持续时间ra.setDuration(200);//罗盘图片使用旋转动画imageView.startAnimation(ra);currentDegree = -degree;}}//传感器精度的改变public void onAccuracyChanged(Sensor sensor, int accuracy) {}}



更多相关文章

  1. android中传感器的学习
  2. Android 重力感应获取手机运动方向和角度
  3. android传感器;摇一摇抽签功能
  4. Android传感器开发基本流程
  5. android 获取屏幕的方向
  6. Android UI技巧(一)——Android中伸缩自如的9patch图片切法,没有美
  7. android 动态设置Activity 的切换方向
  8. android 传感器使用与开发----方向传感器

随机推荐

  1. 成天说要删库跑路,这次真的有人干了
  2. Python+Kepler.gl轻松制作时间轮播地图
  3. 必知必会的8个Python列表技巧
  4. JavaScript的工作原理:V8引擎内部机制及优
  5. 对比Excel,学习pandas数据透视表
  6. 数据分析实战:利用python对心脏病数据集进
  7. 机器学习中最常见的四种分类模型
  8. 基于geopandas的空间数据分析——空间计
  9. Github 30000 Star的免费BI工具:Superset
  10. RocketMQ安装部署