Android uses the sensor to imitate wechat shake function
sensor
Briefly introduce the sensor: it is the hardware used by the device to sense the changes of the surrounding environment.
The sensors in Android are included in the sensor framework and belong to Android. Hardware. * (hardware part)
The sensor frame mainly consists of four parts:
① Sensormanager: it is used to obtain the entrance of the sensor. It is a system service. It can also register and unregister monitoring for the sensor ② sensor: specific sensor, including the name, type and sampling rate of the sensor ③ sensorevent: sensor event, including the data collected by the sensor and the accuracy of the sensor ④ sensoreventlistener: monitoring of the sensor, Mainly monitor sensor data changes, accuracy changes
Android provides two ways to play audio
① Mediaplayer plays regular audio and video, which is usually used on the player. ② soundpool sound pool is usually used for small and frequently played music, which needs to play multiple music at the same time
Vibrate vibration sensor needs to add permission
<uses-permission android:name="android.permission.VIBRATE"/>
Before implementation, let's take a look at the sensors on the mobile phone? (you can skip...) oncreat(); Set textview in
I was surprised to find that there are 41 sensors on Xiaomi 5, which can be ha... Great, okay, let's get back to business - shake it on wechat
In the interface, three pictures of you are stacked, and relativelayout is used for layout
Method encapsulated in oncreate
The two pictures covered above are initialized
When initializing the soundpool, I found that the new soundpool is no longer recommended. The new version (API > 21) is built by builder, so I use the version to judge here
Initialize vibrator: (is a system service)
mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
Shake it. The acceleration sensor is used. You need to monitor its change status. The registration of sensormanager also appears in pairs
The monitoring here needs to be implemented manually (this is not intelligent...) the implements sensoreventlistener will implement two methods
The following is the specific implementation of the method:
Animation has nothing to explain. It's directly on the code
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.