Mega Code Archive

 
Categories / Android / Hardware
 

SensorManager Demo

//main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent">   <TableLayout     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:stretchColumns="0,1,2">     <TableRow       android:layout_weight="1">       <View         android:id="@+id/top"         android:layout_column="1"       />     </TableRow>     <TableRow       android:layout_weight="1">       <View         android:id="@+id/left"         android:layout_column="0"       />       <View         android:id="@+id/right"         android:layout_column="2"       />     </TableRow>     <TableRow       android:layout_weight="1">       <View         android:id="@+id/bottom"         android:layout_column="1"       />     </TableRow>   </TableLayout>   <TextView       android:id="@+id/values"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"   /> </RelativeLayout> package app.test; import android.app.Activity; import android.graphics.Color; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class Test extends Activity implements SensorEventListener {          private SensorManager mSensorManager;     private Sensor mAccelerometer;     private TextView valueView;     private View mTop, mBottom, mLeft, mRight;          public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);         mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);                  valueView = (TextView)findViewById(R.id.values);         mTop = findViewById(R.id.top);         mBottom = findViewById(R.id.bottom);         mLeft = findViewById(R.id.left);         mRight = findViewById(R.id.right);     }     protected void onResume() {         super.onResume();         mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);     }     protected void onPause() {         super.onPause();         mSensorManager.unregisterListener(this);     }     public void onAccuracyChanged(Sensor sensor, int accuracy) { }     public void onSensorChanged(SensorEvent event) {         float[] values = event.values;         float x = values[0]/10;         float y = values[1]/10;         int scaleFactor;                  if(x > 0) {             scaleFactor = (int)Math.min(x*255, 255);             mRight.setBackgroundColor(Color.TRANSPARENT);             mLeft.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));         } else {             scaleFactor = (int)Math.min(Math.abs(x)*255, 255);             mRight.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));             mLeft.setBackgroundColor(Color.TRANSPARENT);         }                  if(y > 0) {             scaleFactor = (int)Math.min(y*255, 255);             mTop.setBackgroundColor(Color.TRANSPARENT);             mBottom.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));         } else {             scaleFactor = (int)Math.min(Math.abs(y)*255, 255);             mTop.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));             mBottom.setBackgroundColor(Color.TRANSPARENT);         }         valueView.setText(String.format("X: %1$1.2f, Y: %2$1.2f, Z: %3$1.2f",                 values[0], values[1], values[2]));     } }