Android Implementation Compass Features

Source: Internet
Author: User

(1) The layout file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Fill_parent"android:layout_height="Fill_parent"  Android:gravity="center"android:orientation="vertical" >                    <ImageViewandroid:id="@+id/imageview"android:layout_width="Wrap _content "android:layout_height=" Wrap_content "android:src=" @drawable/zn " />                                </linearlayout>

Picture required:

(2) Mainactivity.java

Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;ImportAndroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;ImportAndroid.view.animation.Animation;ImportAndroid.view.animation.RotateAnimation;ImportAndroid.widget.ImageView; Public  class mainactivity extends Activity {    PrivateImageView ImageView;PrivateSensormanager Manager;PrivateSensorlistener listener =NewSensorlistener ();@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main); ImageView = (ImageView) This. Findviewbyid (R.id.imageview); Imageview.setkeepscreenon (true);    Manager = (Sensormanager) getsystemservice (Context.sensor_service); }@Override    protected void Onresume() {Sensor sensor = Manager.getdefaultsensor (sensor.type_orientation); Manager.registerlistener (listener, sensor, sensormanager.sensor_delay_game);Super. Onresume (); }@Override    protected void OnPause() {Manager.unregisterlistener (listener);Super. OnPause (); }Private Final  class Sensorlistener implements Sensoreventlistener {        Private floatPredegree =0; Public void onsensorchanged(Sensorevent event) {floatdegree = event.values[0];//Store a directional value ofrotateanimation animation =NewRotateanimation (Predegree,-degree, Animation.relative_to_self,0.5F, Animation.relative_to_self,0.5f); Animation.setduration ( $);            Imageview.startanimation (animation);        Predegree =-degree; } Public void onaccuracychanged(Sensor sensor,intaccuracy) {}}}

The effect is as follows:

Android Implementation Compass Features

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.