Android学习-指南针
效果图(背景图片网上随便找的):
1.编写布局文件activity_main.xml文件
主要放一个东南西北方向图片,一个指针图片用来指明方向
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fff"><ImageViewandroid:id="@+id/imageView2"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="1.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.0"app:srcCompat="@drawable/south_background" /><ImageViewandroid:id="@+id/imageView4"android:layout_width="57dp"android:layout_height="77dp"android:layout_marginStart="177dp"android:layout_marginLeft="177dp"android:layout_marginTop="327dp"android:layout_marginEnd="177dp"android:layout_marginRight="177dp"android:layout_marginBottom="327dp"app:layout_constraintBottom_toBottomOf="@+id/imageView2"app:layout_constraintEnd_toEndOf="@+id/imageView2"app:layout_constraintStart_toStartOf="@+id/imageView2"app:layout_constraintTop_toTopOf="@+id/imageView2"app:srcCompat="@drawable/south_campass" /></androidx.constraintlayout.widget.ConstraintLayout>
2.编写主类MainActivity类
(1)获取传感器管理对象SensorManager
(2)获取指定类型的传感器
(3)注册监听,通过实时监听即可获取传感器传回来的数据
public class MainActivity extends AppCompatActivity implements SensorEventListener {// 定义显示指南针的图片ImageView compassImage;// 记录指南针图片转过的角度float currentDegree = 0f;// 定义Sensor管理器SensorManager mSensorManager;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取界面中显示指南针的图片compassImage = (ImageView) findViewById(R.id.imageView4);// 获取传感器管理服务mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);}@Overrideprotected void onResume() {super.onResume();// 为系统的方向传感器注册监听器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onPause() {// 取消注册mSensorManager.unregisterListener(this);super.onPause();}@Overrideprotected void onStop() {// 取消注册mSensorManager.unregisterListener(this);super.onStop();}@Overridepublic void onSensorChanged(SensorEvent event) {// 获取触发event的传感器类型int sensorType = event.sensor.getType();if (sensorType == Sensor.TYPE_ORIENTATION) {// 获取绕Z轴转过的角度float degree = event.values[0];// 创建旋转动画(反向转过degree度)RotateAnimation ra = new RotateAnimation(currentDegree, -degree,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 设置动画的持续时间ra.setDuration(200);// 运行动画compassImage.startAnimation(ra);currentDegree = -degree;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}
}
链接:https://pan.baidu.com/s/1d310XdWJDJLuvQIrCMO3lQ
提取码:nzsb