Android sensor application-gravity Sensor for rolling ball, android Sensor
I. Problem Description
Android supports up to 11 sensors, and different mobile devices support different types of sensors.
1. Gravity sensor GV-sensor
2. Acceleration sensor G-sensor
3. Magnetic sensor M-sensor
4. Direction sensor O-sensor
5. Linear Acceleration sensor LA-sensor
6. Light Sensor SENSOR_TYPE_LIGHT
7. Temperature Sensor
8. Pressure Sensors
9. Proximity Sensors
10. rotating vector sensor RV-sensor
11. gyroscope sensor Gyro-sensor
The use of sensors is not complex. Just grasp the functions and data parameters of different sensors. Next we will gradually master the use of some major sensors through practice.
First, we use a gravity sensor to implement a rolling ball,
Ii. Case study technical preparation
1,Sensor framework
The components involved in sensor application development are as follows:
Sensor
An instance of a specified sensor. This class provides various methods for you to determine the sensor function.
SensorManager
Use this class to create a sensor service instance. This class provides various methods to access and list sensors, register and deregister sensor event listening, and obtain relevant information. Set the data acquisition rate and calibrate the Sensor
SensorEventListener
Sensor listeners detect sensor data changes and accept sensor events
SensorEvent
Sensor events, information carriers, and detected sensor data
2. Learn about gravity sensor GV-sensor
Measure the gravity applied to devices X, Y, and Z axes. The gravity value is 9.8.
Coordinate data:
Place the mobile phone on the desktop. The default X axis is 0, the default Y axis is 0, and the default Z axis is 9.81. Place the phone down on the desktop, and the Z axis is-9.81.
Tilt the phone to the left, and the X axis is positive. Tilt the phone to the right, and the X axis is negative. Tilt the phone up. the Y axis is negative. Tilt the phone down, and the Y axis is positive.
Iii. Code Implementation
1. Compile the game interface (MySurfaceView) based on SurfaceView)
Public class MySurfaceView extends SurfaceView implements Callback, Runnable {private SurfaceHolder sfh; private Paint paint; private Thread th; private boolean flag; private Canvas canvas; private int screenW, screenH; // declare a Sensor manager private SensorManager sm; // declare a sensor private Sensor sensor; // declare a Sensor listener private SensorEventListener mySensorListener; // round X, Y coordinate private int arc_x, arc_y; // The xyz value of the sensor p Rivate float x = 0, y = 0, z = 0;/*** SurfaceView initialization function */public MySurfaceView (Context context) {super (context); sfh = this. getHolder (); sfh. addCallback (this); paint = new Paint (); paint. setColor (Color. WHITE); paint. setAntiAlias (true); setFocusable (true); // gets the sensor management instance sm = (SensorManager) MainActivity. instance. getSystemService (Service. SENSOR_SERVICE); // instance a gravity sensor instance sensor = sm. getdefasensensor (Sensor. TYPE_ACCELEROMETER); // instance sensor listener mySensorListener = new SensorEventListener () {@ Override // response to this function public void onSensorChanged (SensorEvent event) when the sensor value is changed) {x = event. values [0]; // x> 0 indicates that the current mobile phone is flipped left x <0 y = event. values [1]; // y> 0 indicates that the current mobile phone is flipped down by y <0, and z = event. values [2]; // z> 0 mobile phone screen up z <0 mobile phone screen down arc_x-= x; arc_y + = y ;} @ Override // response to this function public void onAccuracyChanged (Sensor sensor, int ac Curacy) {}}; // register the listener sm for the sensor. registerListener (mySensorListener, sensor, SensorManager. SENSOR_DELAY_GAME);}/*** SurfaceView creation, response to this function */@ Override public void surfaceCreated (SurfaceHolder holder) {screenW = this. getWidth (); screenH = this. getHeight (); flag = true; // instance Thread th = new Thread (this); // start Thread th. start ();}/*** game plotting */public void myDraw () {try {canvas = sfh. lockCanvas (); if (canvas! = Null) {canvas. drawColor (Color. BLACK); paint. setColor (Color. RED); canvas. drawArc (new RectF (arc_x, arc_y, arc_x + 50, arc_y + 50), 0,360, true, paint); paint. setColor (Color. YELLOW); canvas. drawText ("Current gravity sensor value:", arc_x-50, arc_y-30, paint); canvas. drawText ("x =" + x + ", y =" + y + ", z =" + z, arc_x-50, arc_y, paint ); string temp_str = "tony prompt:"; String temp_str2 = ""; String temp_str3 = ""; if (X <1 & x>-1 & y <1 & y>-1) {temp_str + = "the current mobile phone is in a horizontally placed status "; if (z> 0) {temp_str2 + = "and the screen is up";} else {temp_str2 + = "and the screen is down, prompting you not to lie down and play with your phone. It is not good for your eyes ~ ";}} Else {if (x> 1) {temp_str2 + =" the current phone is in the Left flip state ";} else if (x <-1) {temp_str2 + = "the current mobile phone is in the right turn state";} if (y> 1) {temp_str2 + = "the current mobile phone is in the downward turn state ";} else if (y <-1) {temp_str2 + = "the current phone is turning up";} if (z> 0) {temp_str3 + = "and the screen is facing up ";} else {temp_str3 + = "and the screen is down, prompting you not to lie down and play with your cell phone. It's not good for your eyes ~ ";}} Paint. setTextSize (10); canvas. drawText (temp_str, 0, 50, paint); canvas. drawText (temp_str2, 0, 80, paint); canvas. drawText (temp_str3, 0,110, paint) ;}} catch (Exception e) {// TODO: handle exception} finally {if (canvas! = Null) sfh. unlockCanvasAndPost (canvas) ;}}/*** touch screen event listener */@ Override public boolean onTouchEvent (MotionEvent event) {return true ;} /*** button event monitoring */@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {return super. onKeyDown (keyCode, event);}/*** game logic */private void logic () {}@ Override public void run () {while (flag) {long start = System. currentTimeMillis (); myDraw (); logic (); long end = System. currentTimeMillis (); try {if (end-start <50) {Thread. sleep (50-(end-start);} catch (InterruptedException e) {e. printStackTrace () ;}}/ *** SurfaceView view status changed, response to this function */@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}/*** response to this function */@ Override public void surfaceDestroyed (SurfaceHolder holder) {flag = false ;}}
2. Write the main program
Public class MainActivity extends Activity {public static MainActivity instance; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); instance = this; // set full screen this. getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); requestWindowFeature (Window. FEATURE_NO_TITLE); // display the custom SurfaceView setContentView (new MySurfaceView (this ));}}
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation: