In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge points about how Android uses accelerometers to achieve shake function. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1. First get an example of the sensor manager
SensorManager = (SensorManager) context.getSystemService (Context.SENSOR_SERVICE)
two。 Get acceleration sensor through sensor manager
AccelerateSensor = getSensorManager (context) .getDefaultSensor (Sensor.TYPE_ACCELEROMETER)
3. Register the listener for the acceleration sensor
SensorManager.registerListener (listener, sensor, rate)
Parameter description:
Listener: acceleration sensor listener instance
Sensor: an example of acceleration sensor. AccelerateSensor is used to realize Shake.
Rate: sensor response speed, there are four constants selected together
SENSOR_DELAY_NORMAL: matches the change of screen orientation, default sensor speed
SENSOR_DELAY_UI: match user interface
If you update UI, it is recommended to use SENSOR_DELAY_GAME:
For matching games, game developers recommend using SENSOR_DELAY_FASTEST.: matching as fast as possible.
Choose according to the situation, generally choose the first one.
Sensor listener: SensorEventListener has two callback methods
OnSensorChanged (SensorEvent event) and onAccuracyChanged (Sensor sensor, int accuracy)
The first is the corresponding method of sensor value change.
The second is the corresponding method of the change of reaction rate.
Both methods will be called at the same time
Introduction to onSensorChanged (SensorEvent event)
NSensorChanged (SensorEvent event)
The values variable of the event instance is very important, and the values in it have different meanings depending on the sensor. Take the acceleration sensor as an example:
Values this variable is of type float [] array and has up to three elements:
Float x = values [0] represents the X axis
Float y = values [1] represents the Y axis
Float z = values [2] represents the Y axis
The direction of the X axis is from left to right along the horizontal direction of the screen. If the phone is not square, the shorter edges need to be placed horizontally and the longer edges need to be placed vertically. The direction of the Y axis is to point to the top of the screen vertically from the lower left corner of the screen. Put the phone flat on the table, the direction of the Z axis is from the phone to the sky.
We judge whether the phone is shaken or not, as long as the x _ ray y _ r z axis reaches the set threshold, it means shake.
@ Overridepublic void onSensorChanged (SensorEvent event) {int sensorType = event.sensor.getType (); / / values [0]: X axis, values [1]: y axis, values [2]: Z axis float [] values = event.values; float x = values [0]; float y = values [1]; float z = values [2]; Log.i (TAG, "x:" + x + "y:" + y + "z:" + z) Log.i (TAG, "Math.abs (x):" + Math.abs (x) + "Math.abs (y):" + Math.abs (y) + "Math.abs (z):" + Math.abs (z)); if (sensorType = = Sensor.TYPE_ACCELEROMETER) {int value = 15 / / shake the threshold. The maximum value that can be reached by different mobile phones is different. For example, a certain brand of mobile phone can only reach 20 if (x > = value | | x = value | | y = value | | z = 15 | x = 15 | y = 15 | | z |
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.