My Android advanced tour ------> Android uses Sensor (Sensor) to implement the level function.
The level here refers to a more traditional bubble level, filled with liquid in a transparent disc, with a bubble in the liquid. When one end is tilted, this bubble will float at one end.
The first parameter returned by the Direction sensor is used to implement a compass application. My Android advanced tour ------> Android uses Sensor (Sensor) to implement the compass Function
(Address: http://blog.csdn.net/ouyang_peng/article/details/8801204)
Next, we use the second and third parameters returned to implement the level. Because the second parameter reflects the angle of the bottom tilt (when the top is tilted to a negative value), the third parameter can reflect the angle of the right tilt (when the left is tilted to a negative value ). Based on these two perspectives, you can develop a level meter. This is also the realization idea of the level meter. This example comes from the crazy Android handout to see the running effect:
This program customizes a View to draw transparent disks and bubbles, where the position of bubbles changes dynamically. The custom View code is as follows:
MyView. java
Package org. crazyit. sensor; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. util. attributeSet; import android. view. view; public class MyView extends View {// defines the Bitmap back of the dashboard image of the level. // defines the Bitmap bubble icon in the level; // define the X and Y coordinates of bubbles in the Level int bubbleX, bubbleY; public MyView (Context context, AttributeSet attrs) {super (context, attrs ); // load the level image and bubble image back = BitmapFactory. decodeResource (getResources (), R. drawable. back); bubble = BitmapFactory. decodeResource (getResources (), R. drawable. bubble) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // draw a horizontal dashboard image canvas. drawBitmap (back, 0, 0, null); // draw a bubble canvas Based on the bubble coordinates. drawBitmap (bubble, bubbleX, bubbleY, null );}}
Layout file main. xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#fff"><org.crazyit.sensor.MyView android:id="@+id/show"android:layout_width="fill_parent" android:layout_height="fill_parent"/></FrameLayout>
Bubble.png
Back.png
Gradienter. java
Package org. crazyit. sensor; import android. app. activity; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. OS. bundle; public class Gradienter extends Activity implements SensorEventListener {// defines the dashboard MyView show of the Level; // defines the maximum tilt angle that the level can handle. Beyond this angle, bubbles are directly located at the boundary. Int MAX_ANGLE = 30; // defines the Sensor manager SensorManager mSensorManager; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the main component show = (MyView) findViewById (R. id. show); // get Sensor Management Service mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE) ;}@ Overridepublic void onResume () {super. onResume (); // registers the listener mSensorManager for the system's sensor. reg IsterListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ORIENTATION), SensorManager. SENSOR_DELAY_GAME) ;}@ Overrideprotected void onPause () {// cancel mSensorManager registration. unregisterListener (this); super. onPause () ;}@ Overrideprotected void onStop () {// cancel mSensorManager registration. unregisterListener (this); super. onStop () ;}@ Overridepublic void onAccuracyChanged (Sensor sensor, int accuracy) {}@ Overridepublic void onS EnsorChanged (SensorEvent event) {float [] values = event. values; // gets the sensor type of the trigger event. int sensorType = event. sensor. getType (); switch (sensorType) {case Sensor. TYPE_ORIENTATION: // obtain the float yAngle = values [1] From the Y axis; // obtain the float zAngle = values [2] From the Z axis. // when the bubble is in the middle (the level is completely horizontal), the X and Y coordinates of the bubble int x = (show. back. getWidth ()-show. bubble. getWidth ()/2; int y = (show. back. getHeight ()-show. bubble. getHeight ()/2; // If The tilt angle of the axis is still within the maximum angle if (Math. abs (zAngle) <= MAX_ANGLE) {// calculate the variation value of X coordinates based on the skew angle with the Z axis (the larger the skew angle, the larger the X coordinate change) int deltaX = (int) (show. back. getWidth ()-show. bubble. getWidth ()/2 * zAngle/MAX_ANGLE); x + = deltaX;} // If the tilt angle with the Z axis is greater than MAX_ANGLE, bubble should be to the leftmost else if (zAngle> MAX_ANGLE) {x = 0 ;}// if the tilt angle with the Z axis is already less than negative MAX_ANGLE, the bubble should be at the rightmost else {x = show. back. getWidth ()-show. bubble. getWidth ();} // if the tilt angle with the Y axis is still within the maximum angle if (Math. abs (yAngle) <= MAX_ANGLE) {// calculate the variation value of Y coordinate based on the skew angle of Y coordinate with the Y coordinate (the larger the skew angle, the greater the Y coordinate variation) int deltaY = (int) (show. back. getHeight ()-show. bubble. getHeight ()/2 * yAngle/MAX_ANGLE); y + = deltaY;} // If the tilt angle with the y axis is greater than MAX_ANGLE, bubble should be at the bottom of else if (yAngle> MAX_ANGLE) {y = show. back. getHeight ()-show. bubble. getHeight () ;}// if the tilt angle with the y axis is already less than the negative MAX_ANGLE, the bubble should reach the rightmost else {y = 0 ;} // if the calculated X and Y coordinates are still in the gauge, update the if (isContain (x, y) {show. bubbleX = x; s How. bubbleY = y;} // notifies the system to return to the MyView component show. postInvalidate (); break ;}// calculates whether the bubbles at the x and y points are in the gauge private boolean isContain (int x, int y) {// calculates the coordinates of the bubble center X and Yint bubbleCx = x + show. bubble. getWidth ()/2; int bubbleCy = y + show. bubble. getWidth ()/2; // calculates the coordinates of the center X and Yint backCx = show of the horizontal dashboard. back. getWidth ()/2; int backCy = show. back. getWidth ()/2; // calculate the distance between the center of the bubble and the center of the horizontal dashboard. Double distance = Math. sqrt (bubbleCx-backCx) * (bubbleCx-backCx) + (bubbleCy-backCy) * (bubbleCy-backCy); // if the distance between the two centers is smaller than their radius, you can think that the bubble at this point is still in the dashboard if (distance <(show. back. getWidth ()-show. bubble. getWidth ()/2) {return true;} else {return false ;}}}
AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="org.crazyit.sensor"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="10"android:targetSdkVersion="17" /><application android:icon="@drawable/ic_launcher" android:label="@string/app_name"><activity android:name=".Gradienter"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
PS: run this program in the real machine environment. If it is run in the simulator, it may not work.
========================================================== ========================================================== ============================
Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!
Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng
========================================================== ========================================================== ============================
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.