Mask! Android gravity sensor,
The first two articles introduced two very interesting gesture operations. embedded in our game, we have to say that it adds a lot of flexibility, playability, and fun to the game! We will continue to introduce you to the highlights today! Sensor!
I. What is a sensor:
Sensors can detect functions such as light, heat, temperature, gravity, and direction!
Ii. Which sensors are provided in Android:
1. Acceleration Sensor (gravity sensor)
2. gyroscope Sensor
3. Optical Sensors
5. Constant Magnetic Field Sensor
6. Direction Sensor
7. Constant Pressure Sensors
8. Proximity Sensors
9. Temperature Sensor
Today, we will introduce the most common and frequently used sensor in game development,Acceleration Sensor (gravity sensor)!
Because the simulator cannot be tested, I used my mobile phone to debug it. The first two were:
/**
* @author Himi
* @ Sensor acceleration Sensor, also known as gravity Sensor
* @ SDK 1.5 (api 3) supports sensors.
* @ Explanation: This sensor not only detects the player's mobile phone reversal action, but also gets different sensor values based on the extent to which the mobile phone is reversed!
*/
public
class
MySurfaceView
extends
SurfaceView
implements
Callback, Runnable {
private
Thread th =
new
Thread(
this
);
private
SurfaceHolder sfh;
private
Canvas canvas;
private
Paint paint;
private
SensorManager sm;
private
Sensor sensor;
private
SensorEventListener mySensorListener;
private
int
arc_x, arc_y;
// The position of x and y in the circle
private
float
x =
0
, y =
0
, z =
0
;
public
MySurfaceView(Context context) {
super
(context);
this
.setKeepScreenOn(
true
);
sfh =
this
.getHolder();
sfh.addCallback(
this
);
paint =
new
Paint();
paint.setAntiAlias(
true
);
setFocusable(
true
);
setFocusableInTouchMode(
true
);
// Get the sensor management object through the service
sm = (SensorManager) MainActivity.ma
.getSystemService(Service.SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// Get an example of <a title = "a gravity sensor" href = http://www.android100.org/> a gravity Sensor </a>
// Type of the TYPE_ACCELEROMETER accelerometer (gravity sensor.
// TYPE_ALL describes all types of sensors.
// TYPE_GYROSCOPE gyroscope sensor type
// TYPE_LIGHT Optical Sensor Type
// TYPE_MAGNETIC_FIELD constant magnetic field sensor type.
// Type of the sensor in the TYPE_ORIENTATION direction.
// TYPE_PRESSURE describes a constant pressure sensor type
// TYPE_PROXIMITY constant description close sensor
// TYPE_TEMPERATURE temperature sensor type description
mySensorListener =
new
SensorEventListener() {
@Override
// Response to this function when the value obtained by the sensor changes
public
void
onSensorChanged(SensorEvent event) {
// Note 1
// The value obtained by the sensor is changed and processed here
x = event.values[
0
];
// Tumble the phone horizontally
// X> 0 indicates that the current mobile phone is flipped left x <0 indicates that the mobile phone is flipped right.
y = event.values[
1
];
// Scroll up the phone
// Y> 0 indicates that the current mobile phone is flipped down. y <0 indicates that the current mobile phone is flipped down.
z = event.values[
2
];
// Screen orientation
// Z> 0 mobile phone screen up z <0 mobile phone screen down
arc_x -= x;
// Note 2
arc_y += y;
}
@Override
// Response to this function when the sensor's precision changes
public
void
onAccuracyChanged(Sensor sensor,
int
accuracy) {
// TODO Auto-generated method stub
}
};
sm.registerListener(mySensorListener, sensor,
SensorManager.SENSOR_DELAY_GAME);
// The first parameter is the sensor listener, and the second parameter is the sensor instance to be monitored.
// The last parameter is the sensor speed type of the listener. There are four formats in total.
// SENSOR_DELAY_NORMAL
// SENSOR_DELAY_UI
// SENSOR_DELAY_GAME is suitable for games (we must select this option ~)
// SENSOR_DELAY_FASTEST is the fastest
}
public
void
surfaceCreated(SurfaceHolder holder) {
arc_x =
this
.getWidth() /
2
-
25
;
arc_y =
this
.getHeight() /
2
-
25
;
th.start();
}
public
void
draw() {
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 =
"Himi prompt :"
;
String temp_str2 =
""
;
String temp_str3 =
""
;
if
(x <
1
&& x > -
1
&& y <
1
&& y > -
1
) {
temp_str +=
"The current mobile phone is placed horizontally"
;
if
(z >
0
) {
temp_str2 +=
"And screen up"
;
}
else
{
temp_str2 +=
"And the screen is down, prompting you not to lie down and play with your cell phone. It's not good for your eyes ~ "
;
}
}
else
{
if
(x >
1
) {
temp_str2 +=
"The current mobile phone is in the Left flip status"
;
}
else
if
(x < -
1
) {
temp_str2 +=
"The current mobile phone is turning to the right"
;
}
if
(y >
1
) {
temp_str2 +=
"The current mobile phone is turning down"
;
}
else
if
(y < -
1
) {
temp_str2 +=
"The current mobile phone is turning up"
;
}
if
(z >
0
) {
temp_str3 +=
"And screen 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(
20
);
canvas.drawText(temp_str,
0
,
50
, paint);
canvas.drawText(temp_str2,
0
,
80
, paint);
canvas.drawText(temp_str3,
0
,
110
, paint);
}
}
catch
(Exception e) {
Log.v(
"Himi"
,
"draw is Error!"
);
}
finally
{
sfh.unlockCanvasAndPost(canvas);
}
}
@Override
public
void
run() {
// TODO Auto-generated method stub
while
(
true
) {
draw();
try
{
Thread.sleep(
100
);
}
catch
(Exception ex) {
}
}
}
public
void
surfaceChanged(SurfaceHolder holder,
int
format,
int
width,
int
height) {
}
public
void
surfaceDestroyed(SurfaceHolder holder) {
}
}
Note 1:
The onSensorChanged event of SensorEventListener will return the SensorEvent object, which contains the latest Sensor data, and obtain a float [] array through event. values! For different sensor types, the array contains different numbers of elements. The gravity sensor always returns an array with a length of 3, representing the values in the X, Y, and Z directions, respectively. The Z axis indicates whether the screen is facing up or down;
Note that the current mobile phone is still in the vertical or horizontal direction, because this will affect the meaning of X and Y!
If the current mobile phone is a portrait screen:
X> 0 indicates that the current mobile phone is flipped left. x <0 indicates that the mobile phone is flipped right.
Y> 0 indicates that the current mobile phone is flipped down. y <0 indicates that the current mobile phone is flipped down.
If the current mobile phone is a horizontal screen:
X> 0 indicates that the current mobile phone is flipped down x <0
Y> 0 indicates that the current mobile phone is flipped right y <0 Left flip
I would like to remind you that:
1. Consider the player's current mobile phone posture, such as portrait screen and landscape screen.
2. Depending on the screen, although the screen coordinate system will automatically change, the sensor value will not automatically change the coordinate system! So why do the values we retrieve from the sensor indicate different actions when the screen is changed !!! Therefore, during game development, when people perform operations such as character movement and image movement, the positive and negative values of gesture X and Y must be clear! Otherwise, the player will play and vomit (too dizzy !) --,
Note 2:
This should have been arc_x + = x; but because my screen is portrait now! The gesture that causes x> 0 indicates that the player has turned the mobile phone to the left, but the circle on our screen should move according to the reverse of the person. Here, the player turns the mobile phone to the left, we should reduce the X coordinate of the prototype! So here we write arc_x-= x ;!
To sum up: Although this chapter only describes a gravity sensor, It is enough, because if you want to use another sensor, you only need to perform the following steps:
1. Use SensorManager. getdefasensensor (); input a parameter of the sensor you want to get its instance!
2. Register!
3. Process events in the listener!
OK! That's simple.