前段时间,有一位网友发私信给我(@伍歌),问我做过磁场传感器可以做过指南针吗?其实我第一节里面已经说过了,磁场传感器可以做,只是算法比较麻烦,最简单的指南针使用方向传感器做出,但是由于工作关系,一直没有来得及帮助他,现在就写一份简单指南针教程吧,先贴图:
布局文件很简单,就一张指南针的平面图片。
<ImageView
android:id="@+id/main_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/bg" />
算法第一节里面也说过了,values[0]:该值表示方位,也就是手机绕着Z轴旋转的角度。 0表示北(North);90表示东(East);180表示南(South);270表示西(West)。如果values[0]的值正好是这4个值,并且手机是水平放置,表示手机的正前方就是这4个方向。可以利用这个特性来实现电子罗盘。如果还有什么疑问请看第一节内容。
具体方法代码
public void onSensorChanged(SensorEvent event) {
// 如果真机上触发event的传感器类型为水平传感器类型
if (event.sensor.getType() == 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);
// 设置动画结束后的保留状态
ra.setFillAfter(true);
// 启动动画
image.startAnimation(ra);
currentDegree = -degree;
}
}
思路就是获取了values[0],根据values[0]的值去旋转图片。所有代码如下:
public class OrientationActivity extends Activity implements
SensorEventListener {
public static final String TAG = "OrientationActivity方向传感器";
private TextView tv_context;
private Sensor mAccelerometer;
private SensorManager mSensorManager;
// 记录指南针图片转过的角度
private float currentDegree = 0f;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_orien);
infoViews();// 初始化控件
}
private void infoViews() {
// btn = (Button) findViewById(R.id.btn_sensor);
tv_context = (TextView) findViewById(R.id.tv_context);
tv_context.setText("指南针");
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ORIENTATION);
image = (ImageView) findViewById(R.id.main_iv);
}
@Override
protected void onResume() {
if (mAccelerometer != null) {
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(getApplicationContext(), "此设备有方向传感器", 0).show();
} else {
Toast.makeText(getApplicationContext(), "此设备没有方向传感器", 0).show();
}
super.onResume();
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
// 如果真机上触发event的传感器类型为水平传感器类型
if (event.sensor.getType() == 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);
// 设置动画结束后的保留状态
ra.setFillAfter(true);
// 启动动画
image.startAnimation(ra);
currentDegree = -degree;
}
}
}
很简单,但是如果我们需要优化的话,就需要调用
Criteria这个类去加载location信息:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
criteria.setAltitudeRequired(false);//不要求海拔信息
criteria.setBearingRequired(false);//不要求方位信息
criteria.setCostAllowed(true);//是否允许付费
criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
然后去写location:
1. LocationListener location= new LocationListener() {
2.
3. @Override
4. public void onStatusChanged(String provider, int status, Bundle extras) {
5. if (status != LocationProvider.OUT_OF_SERVICE) {
6. updateLocation(mLocationManager
7. .getLastKnownLocation(mLocationProvider));
8. else {
9. mLocationTextView.setText(R.string.cannot_get_location);
10. }
11. }
12.
13. @Override
14. public void onProviderEnabled(String provider) {
15. }
16.
17. @Override
18. public void onProviderDisabled(String provider) {
19. }
20.
21. @Override
22. public void onLocationChanged(Location location) {
23. // 更新位置
24. }
25. };
26. }
这样就可以更精确的调用地理位置,但是我在写的过程中发现一个问题,value[0]这个值是不是从一开始就已经绑定好东南西北,否则怎么可能一开始就指向北了?求大神解释下。。。