简易指南针的实现

知道:
1。SensorManager.getRotationMatrix(R,null,accelerometerValues,magneticValues); 给R数组赋值

Matrix(矩阵) Animation(动画)

2。SensorManager.getOrientation(R,values);
给values赋值

3动画的使用(后续有讲解)

package com.example.commpasstest;

import android.app.SearchManager;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private SensorManager sensorManager;
    private ImageView commpassImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        commpassImg = (ImageView) findViewById(R.id.compass_img);
        sensorManager.registerListener(listener , magneticSensor , SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(listener , accelerometerSensor , SensorManager.SENSOR_DELAY_GAME);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(sensorManager !=null){
            sensorManager.unregisterListener(listener);
        }
    }

    private SensorEventListener listener = new SensorEventListener() {
        float[] accelerometerValues = new float[3];
        float[] magneticValues = new float[3];
        private float lastRotateDegree;

        @Override
        public void onSensorChanged(SensorEvent event) {
        //判断是地磁传感器还是加速度传感器
            if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
                //赋值使用clone方法
                accelerometerValues = event.values.clone();//不使用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,accelerometerValues,magneticValues);
            SensorManager.getOrientation(R,values);

            //将计算出的角度取反,用于旋转指南针背景图
            float rotateDegree = -(float)Math.toDegrees(values[0]);


            if (Math.abs(rotateDegree - lastRotateDegree)>1){
                RotateAnimation animation = new RotateAnimation(lastRotateDegree ,rotateDegree , Animation.RELATIVE_TO_SELF , 0.5f ,
                        Animation.RELATIVE_TO_SELF,0.5f);
                //起始角度,旋转的终止角度 后四个参数用于确定中心点
                animation.setFillAfter(true);
                commpassImg.startAnimation(animation);
                lastRotateDegree = rotateDegree;
            }
          //  Log.d("MainActivity","values[0] is " + Math.toDegrees(values[0]));
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.commpasstest.MainActivity">

    
        
        
        
    <ImageView
        android:src="@drawable/compass"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:id="@+id/compass_img"
        android:layout_centerInParent="true"/>
    <ImageView
        android:layout_width="60dp"
        android:layout_height="110dp"
        android:id="@+id/arrow_img"
        android:layout_centerInParent="true"
        android:src="@drawable/arrow"/>

RelativeLayout>

你可能感兴趣的:(Android)