温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-04-16 10:12:13 来源:亿速云 阅读:276 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关Android如何实现电子罗盘(指南针)方向传感器的应用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

简介

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

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

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

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>

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如何实现电子罗盘(指南针)方向传感器的应用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI