Android实现电子罗盘(指南针)方向传感器的应用

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android实现电子罗盘(指南针)方向传感器的应用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

简介

现在每部AndROId手机里边都会内置有许多传感器,如光照传感器、加速度传感器、地磁传感器、压力传感器、温度传感器等,它们能够监测到各种发生在手机撒花姑娘的物理事件。当然Android系统只是负责将这些传感器所输出的信息传递给我们,然后我们可以利用这些信息去开发一些好玩的应用。

图片神马的在网上搜个指南针图片就好了,方便学习

Android实现电子罗盘(指南针)方向传感器的应用

main.XMl

 <&#63;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> 

MainActivity.java

 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) {    }  } 

总结

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android实现电子罗盘(指南针)方向传感器的应用全部内容,希望文章能够帮你解决Android实现电子罗盘(指南针)方向传感器的应用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。