Android gravity sensing example

Source: Internet
Author: User

Many games in Android use gravity sensing technology,We have studied gravity sensing.

The source is at the bottom left of the screen, and the arrow points to a positive direction. From-10 to 10, the Unit is a floating point number. Consider the following:


 


When the mobile phone screen is placed horizontally up (Z axis to day), the values of (x, y, z) are (0, 0, 10 );

When the mobile phone screen is placed horizontally down (Z axis toward the ground), the values of (x, y, z) are (0, 0,-10 );

When the mobile phone screen is placed on the left (X axis to the day), the values of (x, y, z) are (10, 0, 0 );

The values of (x, y, z) are (0, 10, 0) when the mobile phone is vertical (Y axis to day );

The other analogy is that the day is a positive number, and the day is a negative number.

Using the three values of X, Y, and Z to calculate the trigonometric function, you can precisely detect the motion of the mobile phone.


CodeInstance:

Androidmanifest. xml

<Uses-Permission Android: Name = "android. Hardware. sensor. Accelerometer"/>


The code is very simple. Check the source code directly,Finally, source code download is provided.

Java code:

Package COM. learn. sunboy; </P> <p> Import android. app. activity; <br/> Import android. hardware. sensor; <br/> Import android. hardware. sensorevent; <br/> Import android. hardware. sensoreventlistener; <br/> Import android. hardware. sensormanager; <br/> Import android. OS. bundle; <br/> Import android. view. motionevent; <br/> Import android. widget. toast; </P> <p> public class testsensor extends activity {</P> <p> private sensormanager msensormanager = NULL; <br/> private sensor msensor = NULL; <br/> private float x, y, z; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> msensormanager = (sensormanager) This. getsystemservice (sensor_service); <br/> msensor = msensormanager. getdefasensensor (sensor. type_accelerometer); <br/>}</P> <p> sensoreventlistener lsn = new sensoreventlistener () {</P> <p> @ override <br/> Public void onsensorchanged (sensorevent event) {<br/> X = event. values [sensormanager. data_x]; <br/> Y = event. values [sensormanager. data_y]; <br/> Z = event. values [sensormanager. data_z]; <br/>}</P> <p> @ override <br/> Public void onaccuracychanged (sensor, int accuracy) {<br/> // todo auto-generated method stub </P> <p >}< br/> }; </P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {</P> <p> If (event. getaction () = motionevent. action_down) {</P> <p> msensormanager. registerlistener (LSN, msensor, sensormanager. sensor_delay_game); </P> <p> string STR = "x =" + x + "; y =" + Y + "; Z =" + z; <br/> toast. maketext (getapplicationcontext (), STR, toast. length_long ). show (); <br/>}</P> <p> return Super. ontouchevent (event); <br/>}</P> <p> @ override <br/> Public void onresume () {<br/> msensormanager. registerlistener (LSN, msensor, sensormanager. sensor_delay_game); </P> <p> super. onresume (); <br/>}</P> <p> @ override <br/> Public void onpause () {<br/> msensormanager. unregisterlistener (LSN); </P> <p> super. onpause (); <br/>}</P> <p >}< br/>
Running effect:

Note: This is on the simulator (SDK v1.5). Since the simulator cannot sense gravity, please test it on the physical machine.


Source code download


 

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.