Android studio指南针

package com.example.zhinanzheng2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

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.service.autofill.FieldClassification;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    //通过加速度传感器和地磁传感器编写指南针
    private SensorManager sensorManager;   // 声明一个传感管理器对象
    private ImageView imageView;
    private float lastRotateDegree;
    private int b=0;
    private int c=0;
    //------------------------------------
    private TextView tv_direction;

    private float[] mAcceValues; // 加速度变更值的数组
    private float[] mMagnValues; // 磁场强度变更值的数组
    //------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageview);
        tv_direction=findViewById(R.id.tv_direction);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //加速度感应器
        Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        //地磁感应器
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        sensorManager.registerListener(this, magneticSensor, SensorManager.SENSOR_DELAY_GAME);

        sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

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

    float[] accelerometerValues = new float[3];

    float[] magneticValues = new float[3];

    @Override
    public void onSensorChanged(SensorEvent event) {
        // 判断当前是加速度感应器还是地磁感应器
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            //赋值调用clone方法
            accelerometerValues = event.values.clone();
        } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            //赋值调用clone方法
            magneticValues = event.values.clone();
        }
        float[] R = new float[9];
        float[] values = new float[3];
        SensorManager.getRotationMatrix(R,null,accelerometerValues,magneticValues);
        sensorManager.getOrientation(R, values);
        Log.d("Main","values[0] :"+Math.toDegrees(values[0]));
        //values[0]的取值范围是-180到180度。
        //+-180表示正南方向,0度表示正北,-90表示正西,+90表示正东

        //将计算出的旋转角度取反,用于旋转指南针背景图
        float rotateDegree = -(float) Math.toDegrees(values[0]);
//。。。。。。。。。。。。。。。防颤。。。。。。。。。。。。。。。。。。。
        if (Math.abs(rotateDegree - lastRotateDegree) > 2) {
            RotateAnimation animation = new RotateAnimation(lastRotateDegree,rotateDegree, Animation.RELATIVE_TO_SELF,0.5f,
                    Animation.RELATIVE_TO_SELF,0.5f);
            animation.setFillAfter(true);
            imageView.startAnimation(animation); //动画效果转动传感器
            lastRotateDegree = rotateDegree;
        }
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { // 加速度变更事件
            mAcceValues = event.values;
        } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { // 磁场强度变更事件
            mMagnValues = event.values;
        }
        if (mAcceValues != null && mMagnValues != null) {
            calculateOrientation(); // 加速度和磁场强度两个都有了,才能计算磁极的方向
        }
    }

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

    }

    private void calculateOrientation() {
        float[] values = new float[9];
        float[] R = new float[9];
        int a;
        SensorManager.getRotationMatrix(R, null, mAcceValues, mMagnValues);
        SensorManager.getOrientation(R, values);
        values[0] = (float) Math.toDegrees(values[0]);
        Log.i("aaaaaaaaaaa", values[0] + "");

        // 设置罗盘视图中的指南针方向
        if (Math.abs(b - (int) values[0]) > 2) {                           //。。。。。。。。。。。。。。防颤。。。。。。。。。。。。。。。。。。
            b = (int) values[0];
            if (values[0] >= -10 && values[0] < 10) {
                a = (int) values[0] + 360;

                tv_direction.setText("正北");
            } else if (values[0] >= 10 && values[0] < 80) {

                tv_direction.setText("东北" + (int) values[0] + "°");
            } else if (values[0] >= 80 && values[0] <= 100) {
                tv_direction.setText("正东");
            } else if (values[0] >= 100 && values[0] < 170) {
                tv_direction.setText("东南" + (int) values[0] + "°");
            } else if ((values[0] >= 170 && values[0] <= 180)
                    || (values[0]) >= -180 && values[0] < -170) {
                a = (int) values[0] + 360;
                tv_direction.setText("正南");
            } else if (values[0] >= -170 && values[0] < -100) {
                a = (int) values[0] + 360;
                tv_direction.setText("西南" + a + "°");
            } else if (values[0] >= -100 && values[0] < -80) {
                a = (int) values[0] + 360;
                tv_direction.setText("正西");
            } else if (values[0] >= -80 && values[0] < -10) {
                a = (int) values[0] + 360;
                tv_direction.setText("西北" + a + "°");
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
    android:background="@drawable/abcatdp"
        android:gravity="center">
    <TextView
        android:id="@+id/tv_direction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="指南针找方向来啦"
        android:textColor="@color/white"
        android:textSize="40sp" />
       <RelativeLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
               <ImageView
                   android:id="@+id/imageview"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_centerInParent="true"
                   android:src="@drawable/di1_pan2" />
               <ImageView
                   android:id="@+id/imageview2"
                   android:layout_width="311px"
                   android:layout_height="312px"
                   android:layout_centerInParent="true"
                   android:src="@drawable/zhi2_zheng1" />
       </RelativeLayout>


</LinearLayout>

 abcatdp.jpg

 di1_pan2.png

 zhi2_zheng1.png

上面都是抠的图,有更好的图可以自行替换 

可选:

 

 

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值