Android uses directional sensors to implement the compass specific steps _android

Source: Internet
Author: User
Step1:Create a new project compass and import a compass picture into the res/drawable-hdpi directory

Step2:UI interface for design applications, Main.xml
Copy Code code as follows:

<span style= "font-size:18px" ><strong><?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:gravity= "Center"
>
<imageview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/compass"
Android:id= "@+id/imageview"
/>
</LinearLayout></STRONG></SPAN>

Step3:Mainactivity.java
Copy Code code as follows:

Import android.app.Activity;
Import Android.content.Context;
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 Mainactivity extends activity {
Private ImageView ImageView;
/** Sensor Manager */
Private Sensormanager Manager;
Private Sensorlistener listener = new Sensorlistener ();
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ImageView = (ImageView) This.findviewbyid (R.id.imageview);
Imageview.setkeepscreenon (TRUE);//Screen highlighting
Get system service (Sensor_service) returns a Sensormanager object
Manager = (Sensormanager) getsystemservice (Context.sensor_service);
}
@Override
protected void Onresume () {
/**
* Get direction sensor
* Get the corresponding sensor type object through the Sensormanager object
*/
Sensor Sensor = Manager.getdefaultsensor (sensor.type_orientation);
Apply to register listeners at the front desk
Manager.registerlistener (listener, sensor,
Sensormanager.sensor_delay_game);
Super.onresume ();
}
@Override
protected void OnPause () {
The application does not destroy the listener at the front
Manager.unregisterlistener (listener);
Super.onpause ();
}
Private Final class Sensorlistener implements Sensoreventlistener {
private float predegree = 0;
@Override
public void Onsensorchanged (Sensorevent event) {
/**
* Values[0]: X-axis direction acceleration
VALUES[1]: Y-axis direction acceleration
VALUES[2]: Z-axis direction acceleration
*/
Float degree = event.values[0];//Store directional value
/** Animation Effect * *
rotateanimation animation = new Rotateanimation (Predegree, Degree,
animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
Animation.setduration (200);
Imageview.startanimation (animation);
Predegree=-degree;

/**
float x=event.values[sensormanager.data_x];
float y=event.values[sensormanager.data_y];
float Z=event.values[sensormanager.data_z];
LOG.I ("XYZ", "x=" + (int) x+ ", y=" + (int) y+ ", z=" + (int) z);
*/
}
@Override
public void onaccuracychanged (Sensor Sensor, int accuracy) {
}
}
}

Step4:Androidmanifest.xml
Copy Code code as follows:

<span style= "font-size:18px" ><strong><?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= " Cn.roco.sensor "
android:versioncode=" 1 "
Android:versionname=" 1.0 ">
<USES-SDK android: minsdkversion= "8"/>
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= "mainactivity"
android:label= "@string/app_name" >
<intent-filter>
& Lt;action android:name= "Android.intent.action.MAIN"/>
<category android:name= " Android.intent.category.LAUNCHER "/>
</intent-filter>
</activity>
</application>
</manifest></STRONG></SPAN>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.