您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
88_采用传感器获取手机的方向
发布时间:2021-03-28 14:34:04编辑:雪饮阅读()
这次要利用安卓中的加速度感应器实现下指南针应用。首先布局上我们需要一个指南针的图片
然后我们需要布局上有一个日志显示区域,便于测试加速度感应器。所以布局文件如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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:src="@drawable/zn"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:src="@drawable/zn"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
那么接下来就是在逻辑层获取感应器,并对感应器创建一个监听事件,用来监听感应器所监听到的数据感应变化值,根据其值我们就可以实现自己的业务了(让指南针图片进行旋转),则逻辑如MainActivity.java:
package com.example.pointer;
import androidx.appcompat.app.AppCompatActivity;
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;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
SensorManager manager;
private ImageView iv;
MyListener listener;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) this.findViewById(R.id.iv);
tv = (TextView) this.findViewById(R.id.textView);
listener=new MyListener();
manager=(SensorManager) getSystemService(SENSOR_SERVICE);
// Sensor sensor=manager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
//上面这个写法是以前的
Sensor sensor=manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//manager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_UI);
//manager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_FASTEST);
//manager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_GAME);
//manager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_NORMAL);
//这四种不同类型的传感器注册频率类型,主要区别于对onSensorChanged触发的速度的快慢,按速度由快到慢可以排序如:
//SENSOR_DELAY_FASTEST最灵敏,快的然你无语
//SENSOR_DELAY_GAME游戏的时候用这个,不过一般用这个就够了
//SENSOR_DELAY_NORMAL比较慢。
//SENSOR_DELAY_UI最慢的
manager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_UI);
}
private class MyListener implements SensorEventListener{
float predegree=0;
float degree;
@Override
public void onSensorChanged(SensorEvent event) {
degree=event.values[0];
String log1="本次旋转角度范围:"+predegree+"---"+degree;
System.out.println(log1);
tv.setText(log1);
System.out.println("开始的角度"+predegree);
System.out.println("跟北的角度为"+degree);
//Animation.RELATIVE_TO_SELF,0.5f,即等于view.getWidth*0.5
//所以这里的意思可以理解为创建旋转动画:
//从开始角度predegree,到结束角度degree
//开始坐标x为待旋转图形宽度的一半(0.5f),开始坐标y为图形高度的一半(0.5f)
RotateAnimation ra = new RotateAnimation(predegree, degree,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(200);
iv.startAnimation(ra);
//重新初始化到图片旋转的开始位置
// predegree=-predegree;
}
// 当传感器的精度变化时
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}
}
那么最后部署到设备中则如:
那么这个项目其实还有点小瑕疵了,感觉就是我手机晃动比较大的时候,旋转的角度也很少,比如我晃动的大概有180度,结果日志显示的好像都是在30左右,明显有点低了。这个瑕疵,这里暂且先不讨论了。个人感觉和每个设备的传感器对应的类型的敏感度有关。或者还有其它原因,这里暂且先不讨论那么多了。
关键字词:android,传感器,指南针