Steps for Android to use a direction sensor to implement a compass

Source: Internet
Author: User

Step 1:Create a new project Compass and import a Compass image to the res/drawable-hdpi directory.

Step 2:Design the application UI, main. xml Copy codeThe Code is 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>

Step 3:MainActivity. javaCopy codeThe Code is 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 highlight
// Obtain the System Service (SENSOR_SERVICE) and return a SensorManager object.
Manager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );
}
@ Override
Protected void onResume (){
/**
* Obtain the direction sensor.
* Get the SensorManager object.
*/
Sensor sensor = manager. getdefasensensor (Sensor. TYPE_ORIENTATION );
// The app registers the listener at the front-end.
Manager. registerListener (listener, sensor,
SensorManager. SENSOR_DELAY_GAME );
Super. onResume ();
}
@ Override
Protected void onPause (){
// Destroy the listener when the application is not in the foreground
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 Acceleration
Values [1]: acceleration in y-axis direction
Values [2]: acceleration in the z-axis direction
*/
Float degree = event. values [0]; // stores the direction 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 ){
}
}
}

Step 4:AndroidManifest. xmlCopy codeThe Code is 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" type = "codeph" text = "/codeph">
<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>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
</Manifest> </STRONG> </SPAN>

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.