首页 > 代码库 > 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"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="${relativePackage}.${activityClass}" >
 6 
 7     <ImageView
 8         android:id="@+id/iv_arrow"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_centerInParent="true"
12         android:src="http://www.mamicode.com/@drawable/arrow3" />
13 
14     <ImageView
15         android:id="@+id/iv_compass"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_centerInParent="true"
19         android:src="http://www.mamicode.com/@drawable/compass" />
20 
21 </RelativeLayout>

源码下载地址:http://pan.baidu.com/s/1i4HIMSP

Android_方向传感器