Android uses sensor to realize the level function
The level introduced here refers to the more traditional bubble level. A transparent disc is filled with liquid, and there is a bubble in the liquid. When one end is tilted, the bubble will float to the tilted end.
Using the first parameter returned by the direction sensor, a small application of compass is realized.
My android advanced journey -------- > Android uses sensor to realize compass function
Next, we use the returned second and third parameters to realize the level. Because the second parameter reflects the tilt angle of the bottom (negative value when the top is tilted), the third parameter can reflect the tilt angle of the right (negative value when the left is tilted). According to these two angles, the level can be developed to realize which end the mobile phone tilts up and the bubble floats to which end. This is also the realization idea of the level. This example is from the crazy Android handout
Let's take a look at the operation effect:
The program has customized a view to draw transparent disks and bubbles, in which the position of bubbles will change dynamically. The custom view code is as follows:
MyView.java
Layout file main.xml
source material:
bubble.png:
back.png :
Gradienter.java
AndroidManifest.xml
PS: please run this program in the real machine environment. If you run it in the simulator, it may not work.
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