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
上面都是抠的图,有更好的图可以自行替换
可选:
- 如果要删除标签,可以看我的这个Android (全屏)去除标题及更改标题栏、时间栏的颜色_ABCat~的博客-CSDN博客_android 修改标题栏颜色
- 如果改标签名字
- app图标设置 找我的文章,写的时候在审批,我有时间更新这条
- 手机测试 电脑没有加速度那些传感器
- 打开开发者模式(每个手机各有不同,华为:设置--系统--关于手机--连点三下版本号;)
- 点开开发人员选项----打开USB调试
- 连接USB线 选择mtp模式
- 成品