Level sensor returned by the first parameter value is to represent the phone turned angle around the Z-axis, which is the top of the phone and the angle between true north. In the program by checking the angle compass application can be achieved. In fact, the idea is very simple, first prepare a picture, the picture pointer direction north. Then develop a direction sensor detects when the program detects the phone number at the top of the angle around the Z-axis turn, let the number of degrees compass picture reverse turn, thus achieving a pointer always points to true north. This is also the principle of the compass. Code is as follows:
Activity:
[Java]
package com.home.compass;
import android.app.Activity;
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;
public class CompassTestActivity extends Activity implements
SensorEventListener {
/ / Define the display assembly compass picture
private ImageView image;
/ / Record the compass picture angle turned
private float currentDegree = 0f;
/ / Define the real machine Sensor Manager
private SensorManager mSensorManager;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
image = (ImageView) findViewById (R.id.main_iv);
/ / Get the real machine sensor management services
mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE);
}
@ Override
protected void onResume () {
super.onResume ();
/ / For the system's orientation sensor registered listeners
mSensorManager.registerListener (this,
mSensorManager.getDefaultSensor (Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@ Override
protected void onPause () {
super.onPause ();
/ / Unregister
mSensorManager.unregisterListener (this);
}
@ Override
public void onAccuracyChanged (Sensor sensor, int accuracy) {
}
@ Override
public void onSensorChanged (SensorEvent event) {
/ / If the event is triggered on a real machine sensor type level sensor type
if (event.sensor.getType () == Sensor.TYPE_ORIENTATION) {
/ / Get the angle around the Z-axis rotated
float degree = event.values [0];
/ / Create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation (currentDegree,-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/ / Set the duration of the animation
ra.setDuration (200);
/ / Set the animation after the end of the reservation status
ra.setFillAfter (true);
/ / Start the animation
image.startAnimation (ra);
currentDegree =-degree;
}
}
}
Layout XML:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: gravity = "center">
<ImageView
android: id = "@ + id / main_iv"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: src = "@ drawable / znz" />
</ LinearLayout>
Here attached a picture of the compass:
Activity:
[Java]
package com.home.compass;
import android.app.Activity;
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;
public class CompassTestActivity extends Activity implements
SensorEventListener {
/ / Define the display assembly compass picture
private ImageView image;
/ / Record the compass picture angle turned
private float currentDegree = 0f;
/ / Define the real machine Sensor Manager
private SensorManager mSensorManager;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
image = (ImageView) findViewById (R.id.main_iv);
/ / Get the real machine sensor management services
mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE);
}
@ Override
protected void onResume () {
super.onResume ();
/ / For the system's orientation sensor registered listeners
mSensorManager.registerListener (this,
mSensorManager.getDefaultSensor (Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@ Override
protected void onPause () {
super.onPause ();
/ / Unregister
mSensorManager.unregisterListener (this);
}
@ Override
public void onAccuracyChanged (Sensor sensor, int accuracy) {
}
@ Override
public void onSensorChanged (SensorEvent event) {
/ / If the event is triggered on a real machine sensor type level sensor type
if (event.sensor.getType () == Sensor.TYPE_ORIENTATION) {
/ / Get the angle around the Z-axis rotated
float degree = event.values [0];
/ / Create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation (currentDegree,-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/ / Set the duration of the animation
ra.setDuration (200);
/ / Set the animation after the end of the reservation status
ra.setFillAfter (true);
/ / Start the animation
image.startAnimation (ra);
currentDegree =-degree;
}
}
}
Layout XML:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: gravity = "center">
<ImageView
android: id = "@ + id / main_iv"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: src = "@ drawable / znz" />
</ LinearLayout>
Here attached a picture of the compass:
No comments:
Post a Comment