Android-oriented sensor learning-Compass example

Source: Internet
Author: User

I just followed the video to learn the operation examples of sensors in Android, and made a very simple compass application using the direction sensor... Sensor functions are rarely used in work projects, so many of them don't know. Now I want to learn more about it as a step-by-step approach...

First paste the software for final operation

The image material used in it is a temporary PPT. It is very simple and can be used. This is not the point.

The code below starts...

Layout file main. xml

<?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:orientation="vertical"    android:background="@android:color/white"    android:gravity="center" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/img" /></LinearLayout>

It is very simple. There is only one imageview control in it.

Mainaactivity class

Package COM. and. sensor; 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 {imageview imgview; sensormanager manager; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imgview = (imageview) findviewbyid (R. id. IMG); imgview. setkeepscreenon (true); Manager = (sensormanager) getsystemservice (context. sensor_service) ;}@ overrideprotected void onresume () {sensor = manager. getdefasensensor (sensor. type_orientation); manager. registerlistener (sensorlistener, sensor, sensormanager. sensor_delay_game); super. onresume ();} sensoreventlistener sensorlistener = new sensoreventlistener () {private float predegree = 0; Public void onsensorchanged (sensorevent event) {float degree = event. values [0]; // The first number in the array is the rotateanimation anim = new rotateanimation (predegree,-degree, animation. relative_to_self, 0.5f, animation. relative_to_self, 0.5f); anim. setduration (200); // imgview. setanimation (anim); // This sentence is incorrect. startanimation (anim); predegree =-degree; // record the starting angle of this time as the initial angle of next rotation} public void onaccuracychanged (sensor, int accuracy ){}}; @ overrideprotected void onpause () {super. onpause (); manager. unregisterlistener (sensorlistener );}}

Note This sentence

RotateAnimation anim = new RotateAnimation(predegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

The first two parameters, the first is the initial rotation angle, and the second is the rotation angle, because the sensor determines the angle to the top of the mobile phone. Imagine if the angle is 90 degrees, and the starting "North" points to the top, 90 degrees, the image needs to rotate 90 degrees counterclockwise, so the second parameter is negative. I still don't understand why it turns clockwise Instead of clockwise. Please kindly enlighten me

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.