This article illustrates the gravitational induction effect of Android programming. Share to everyone for your reference, specific as follows:
Many of the game's games in Android use the technology of gravitational induction, which is a study of gravity sensing
With the bottom left of the screen as the origin, the arrow is pointing in a positive direction. From-10 to 10, with floating-point numbers as a unit, imagine the following scenario:
The value of (X,y,z) is (0,0,10) when the mobile screen is horizontally positioned upward (z-axis);
The value of (X,y,z) is (0,0,-10) when the cell phone screen is placed downward (z-axis) horizontally;
When the cell phone screen is placed on the left (x axis), the value (x,y,z) is (10,0,0);
When the cell phone is upright (y-axis) upward, the value (x,y,z) is (0,10,0) respectively;
Other such analogy, the rule is: the sky is positive, the ground is negative.
Using x,y,z three values for trigonometric functions, you can accurately detect the mobile phone's state of motion.
Code instance:
Androidmanifest.xml
Copy Code code as follows:
<uses-permission android:name= "Android.hardware.sensor.accelerometer"/>
The code is very simple, look directly at the source, and finally provide the source download
Java Code:
Package com.learn.sunboy;
Import android.app.Activity;
Import Android.hardware.Sensor;
Import android.hardware.SensorEvent;
Import Android.hardware.SensorEventListener;
Import Android.hardware.SensorManager;
Import Android.os.Bundle;
Import android.view.MotionEvent;
Import Android.widget.Toast;
public class Testsensor extends activity{private Sensormanager msensormanager = null;
Private Sensor msensor = null;
private float x, y, Z;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Msensormanager = (Sensormanager) this.getsystemservice (Sensor_service);
Msensor = Msensormanager.getdefaultsensor (Sensor.type_accelerometer); Sensoreventlistener LSN = new Sensoreventlistener () {@Override public void onsensorchanged (Sensorevent event)
{x = event.values[sensormanager.data_x];
y = event.values[sensormanager.data_y];
z = event.values[sensormanager.data_z]; } @Override public voidOnaccuracychanged (Sensor Sensor, int accuracy) {//TODO auto-generated Method stub}}; @Override public boolean ontouchevent (Motionevent event) {if (event.getaction () = = Motionevent.action_down) {MS
Ensormanager.registerlistener (LSN, Msensor, sensormanager.sensor_delay_game); String str = "x=" + x + "; Y= "+ y +";
z= "+ Z;
Toast.maketext (Getapplicationcontext (), str, toast.length_long). Show ();
Return Super.ontouchevent (event); @Override public void Onresume () {Msensormanager.registerlistener (LSN, Msensor, Sensormanager.sensor_delay_game)
;
Super.onresume ();
@Override public void OnPause () {Msensormanager.unregisterlistener (LSN);
Super.onpause (); }
}
Operation Effect:
Note: This is the screenshot on the simulator (SDK V1.5), because the simulator can not sense gravity, please test on the physical machine
Full instance code code click here to download the site.
I hope this article will help you with the Android program.