Home>

The so-called sensors can detect functions such as light, heat, temperature, gravity, direction, etc.This article introduces the rolling pinball that the gravity sensor of android sensor application implements.Friends interested in android gravity sensor related knowledge

Familiar:

What is a sensor:

The so-called sensors can detect functions such as light, heat, temperature, gravity, direction, and more!

What are the sensors available in android:

Acceleration sensor (gravity sensor)

Gyroscope sensor

Light sensor

5. Constant magnetic field sensor

6. Direction sensor

7. Constant pressure sensor

8. Proximity sensor

9. Temperature sensor

I. Problem description

There are up to 11 sensors in android,Different mobile phone devices support different types of sensors

1.Gv-sensor

2.Acceleration sensor g-sensor

3. Magnetic sensor m-sensor

4.Orientation sensor o-sensor

5, linear acceleration sensor la-sensor

6, light sensor sensor_type_light

7.Temperature sensor

8.Pressure sensor

9. Proximity sensor

10.Rotary vector sensor rv-sensor

11.Gyro-sensor

The use of sensors is not complicated,Just master the role and data parameters of different sensors,Below we will gradually master the use of some major sensors through practice

First we use gravity sensors to complete the rolling pinball,as the picture shows

Second, case technical preparation

1.Sensor frame

The components involved in sensor application development are as follows:

sensor

An instance of the specified sensor.This class provides various methods for you to determine the capabilities of the sensor.

sensormanager

Use this class to create an instance of the sensor service.This class provides various method classes to access and enumerate sensors,Register and unregister sensor event listeners,And get the corresponding information.Set the data acquisition rate,And calibration sensors

sensoreventlistener

Sensor listener,Detecting changes in sensor data,And accept sensor events

sensorevent

Sensor events,Information carrier,Data from detected sensors

2.Understanding the gravity sensor gv-sensor

Measure the gravity applied to the x, y, and z axes of the device,Gravity value is 9.8

Coordinate data:

Place your phone flat on your desktop,The x-axis defaults to 0, the y-axis defaults to 0, and the z-axis defaults to 9.81. Place your phone face down on your desktop.The z-axis is -9.81.

tilt Tilt your phone to the left,The x-axis is positive.Tilt your phone to the right,The x-axis is negative.Tilt the phone up,The y-axis is negative.Tilt the phone down,The y-axis is positive.

Third, the code implementation

1. Writing game interface based on surfaceview (mysurfaceview)

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;
 //Circular x, y coordinates
 private int arc_x, arc_y;
 //XYZ value of the sensor
 private 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);
 //Get the sensor management class instance
 sm=(sensormanager) mainactivity.instance.getsystemservice (service.sensor_service);
 //Instance a gravity sensor instance
 sensor=sm.getdefaultsensor (sensor.type_accelerometer);
 //Instance sensor listener
 mysensorlistener=new sensoreventlistener () {
  @override
  //Respond to this function when the sensor gets a value change
  public void onsensorchanged (sensorevent event) {
  x=event.values ​​[0];
  //x>0 indicates that the current phone is turned left x<0 is turned right
  y=event.values ​​[1];
  //y>0 indicates that the current phone is turned down y<0 is turned up
  z=event.values ​​[2];
  //z>0 phone screen is facing up z<0 phone screen is facing down
  arc_x-= x;
  arc_y +=y;
  }
  @override
  //Respond to this function when the accuracy of the sensor changes
  public void onaccuracychanged (sensor sensor, int accuracy) {
  }
 };
 //Register the listener for the sensor
 sm.registerlistener (mysensorlistener, sensor, sensormanager.sensor_delay_game);
 }
 /**
 * surfaceview view creation,Respond 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 drawing
 * /
 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 value of the gravity sensor:", 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 phone is currently placed horizontally";
   if (z>0) {
   temp_str2 +="and screen up";
   } else {
   temp_str2 +="and the screen is facing down,Tips not to play with your phone while lying down,Not good for the eyes ~ ";
   }
  } else {
   if (x>1) {
   temp_str2 +="The phone is currently turned left";
   } else if (x<-1) {
   temp_str2 +="The phone is currently turned to the right";
   }
   if (y>1) {
   temp_str2 +="The current phone is turned down";
   } else if (y<-1) {
   temp_str2 +="The current phone is turned up";
   }
   if (z>0) {
   temp_str3 +="and screen up";
   } else {
   temp_str3 +="and the screen is facing down,Tips not to play with your phone while lying down,Not good for the 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 monitoring
 * /
 @override
 public boolean ontouchevent (motionevent event) {
 return true;
 }
 /**
 * Key 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 state changes,Respond to this function
 * /
 @override
 public void surfacechanged (surfaceholder holder, int format, int width, int height) {
 }
 /**
 * When the surfaceview view dies,Respond 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 a custom surfaceview view
 setcontentview (new mysurfaceview (this));
 }
}
  • Previous Implementation method of dynamically creating form and submitting in JavaScript
  • Next Common low-level optimization tutorials and related suggestions for MySQL