指南针的实现原理:
利用android的TYPE_ORIENTATION传感器和旋转动画实现指南针。
方向传感器的event.values是一个float类型的长度为3的数组。
float azimuth_angle = event.values[0];//在z轴旋转度
float pitch_angle = event.values[1];//在x轴旋转度
float roll_angle = event.values[2];//在y轴旋转度
根据api的解释:
Azimuth (degrees of rotation around the z axis). This is the angle between magnetic north and the device’s y axis. For example, if the device’s y axis is aligned with magnetic north this value is 0, and if the device’s y axis is pointing south this value is 180. Likewise, when the y axis is pointing east this value is 90 and when it is pointing west this value is 270.
这是磁北的夹角和设备的y轴。例如,如果设备的y轴与磁北这个值为0,如果设备的y轴指向南这个值是180。同样地,当y轴指向东指向西方时这个值是90,这个值是270。
Pitch (degrees of rotation around the x axis). This value is positive when the positive z axis rotates toward the positive y axis, and it is negative when the positive z axis rotates toward the negative y axis. The range of values is 180 degrees to -180 degrees.
值的范围是180度到-180度。
Roll (degrees of rotation around the y axis). This value is positive when the positive z axis rotates toward the positive x axis, and it is negative when the positive z axis rotates toward the negative x axis. The range of values is 90 degrees to -90 degrees.
值的范围是90度到-90度。
当手机水平放置时,图片旋转的度数就是-azimuth_angle
2.在主布局里放一张指南针的图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cindy.compass.MainActivity">
<ImageView
android:id="@+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/compass"/>
</LinearLayout>
3.MainActivity.java
package com.cindy.compass;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager;
private Sensor sensor_o;
float degree = 0f;
private float currentDegree = 0f;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
imageView = (ImageView)findViewById(R.id.compass);
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()){
case Sensor.TYPE_ORIENTATION:
degree = event.values[0];
float azimuth_angle = event.values[0];//在z轴旋转度
float pitch_angle = event.values[1];//在x轴旋转度
float roll_angle = event.values[2];//在y轴旋转度
break;
}
/*
RotateAnimation类:旋转变化动画类
参数说明:
fromDegrees:旋转的开始角度。
toDegrees:旋转的结束角度。
pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotXValue:X坐标的伸缩值。
pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotYValue:Y坐标的伸缩值
*/
RotateAnimation ra = new RotateAnimation(currentDegree,-degree,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(200);
imageView.startAnimation(ra);
currentDegree = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
sensor_o = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sensorManager.registerListener(this, sensor_o, SensorManager.SENSOR_DELAY_NORMAL);//注册指南针传感器的监听器
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this, sensor_o);//注销传感器
}
}
应用程序下载地址:android指南针