Android_方向传感器

大鹏一日同风起,扶摇直上九万里。这篇文章主要讲述Android_方向传感器相关的知识,希望能为你提供帮助。
android方向传感器小案例,主要代码如下:

package com.hb.direction; 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 SensorManager sm; private ImageView iv_compress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_compress=(ImageView) findViewById(R.id.iv_compass); sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //地磁场 Sensor magnetic = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //加速度 Sensor acceleromter = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sm.registerListener(listener, magnetic, SensorManager.SENSOR_DELAY_GAME); sm.registerListener(listener, acceleromter, SensorManager.SENSOR_DELAY_GAME); } @Override protected void onDestroy() { super.onDestroy(); if( sm!=null){ sm.unregisterListener(listener); } } private SensorEventListener listener=new SensorEventListener() { float[] magneticValues=new float[3]; float[] acceleromterValues=new float[3]; private float lastRotateDegree; @Override public void onSensorChanged(SensorEvent event) { //判断当前是加速度还是地磁场传感器 if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){ acceleromterValues=event.values.clone(); }else if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){ magneticValues=event.values.clone(); } float [] R=new float[9]; float [] values=new float[3]; SensorManager.getRotationMatrix(R, null, acceleromterValues, magneticValues); SensorManager.getOrientation(R, values); //toDegreess转化为度 //tv_direction.setText("values is"+Math.toDegrees(values[0])); //Toast.makeText(MainActivity.this, "values is"+Math.toDegrees(values[0]), 0).show(); //将计算出的旋转角取反用于指南针背景图 float roteteDegree=-(float)Math.toDegrees(values[0]); if((Math.abs(roteteDegree)-lastRotateDegree)> 1){ RotateAnimation animation = new RotateAnimation(lastRotateDegree, roteteDegree,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f); animation.setFillAfter(true); iv_compress.startAnimation(animation); lastRotateDegree=roteteDegree; } }@Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; }

布局文件xml:
1 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5tools:context="${relativePackage}.${activityClass}" > 6 7< ImageView 8android:id="@+id/iv_arrow" 9android:layout_width="wrap_content" 10android:layout_height="wrap_content" 11android:layout_centerInParent="true" 12android:src="https://www.songbingjia.com/android/@drawable/arrow3" /> 13 14< ImageView 15android:id="@+id/iv_compass" 16android:layout_width="wrap_content" 17android:layout_height="wrap_content" 18android:layout_centerInParent="true" 19android:src="https://www.songbingjia.com/android/@drawable/compass" /> 20 21 < /RelativeLayout>

【Android_方向传感器】源码下载地址:http://pan.baidu.com/s/1i4HIMSP

    推荐阅读