SoFunction
Updated on 2025-03-01

Example of usage of direction sensors for Android programming

This article describes the usage of direction sensors in Android programming. Share it for your reference, as follows:

/**
  * Sensor pointer Demo
  *
  * @description:
  * @author ldm
  * @date 2016-4-25 5:29:18 pm
  */
public class SensorHandActivity extends GraphicsActivity {
  // Sensor management object  private SensorManager mSensorManager;
  // Sensor class  private Sensor mSensor;
  // Customize the view of the pointer  private MyCompassView mView;
  /**
    * Induction values ​​detected by the direction sensor values[0]: Azimuth, angle between the north direction of the geomagnetic and the y-axis, rotates around the z-axis (0 to 359).  0=North,
    * 90=East, 180=South, 270=West values[1]: Pitch, rotate around the X-axis (-180 to 180),
    * It is positive when the Z-axis Y-axis moves values[2]: Roll, rotates around the Y-axis (-90 to 90), and positive when the X-axis moves
    */
  private float[] mValues;
  // Sensor monitoring  private final SensorEventListener mSensorListener = new SensorEventListener() {
    public void onSensorChanged(SensorEvent event) {
      mValues = ;
      if (mView != null) {
        ();
      }
    }
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
  };
  @SuppressWarnings("deprecation")
  @Override
  protected void onCreate(Bundle icicle) {
    (icicle);
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = (Sensor.TYPE_ORIENTATION);
    mView = new MyCompassView(this);
    setContentView(mView);
  }
  @Override
  protected void onResume() {
    ();
    /**
      * Register sensor listening events in onResume method
      * The first parameter: listen for Sensor events, the second parameter is the value of the Sensor target type, and the third parameter is the accuracy density of the delay time.  Precision parameters for delay time Parameters
      * Delay time SensorManager.SENSOR_DELAY_FASTEST 0ms
      * SensorManager.SENSOR_DELAY_GAME 20ms SensorManager.SENSOR_DELAY_UI
      * 60ms SensorManager.SENSOR_DELAY_NORMAL 200ms
      */
    (mSensorListener, mSensor,
        SensorManager.SENSOR_DELAY_GAME);
  }
  @Override
  protected void onStop() {
    // Unregister listening in the onStop method    (mSensorListener);
    ();
  }
  private class MyCompassView extends View {
    // Define the brush Paint    private Paint mPaint;
    // Define the path Path to draw the pointer    private Path mPath;
    public MyCompassView(Context context) {
      super(context);
      initPaintAndPath();
    }
    private void initPaintAndPath() {
      // Initialize the brush      mPaint = new Paint();
      (true);
      ();
      ();
      // Initialize the drawing path      mPath = new Path();
      (0, -50);// Move to the point      (-20, 60);// Connect the specified point with lines      (0, 50);
      (20, 60);
      ();// Close the path    }
    @Override
    protected void onDraw(Canvas canvas) {
      // Set the screen background      ();
      int w = ();
      int h = ();
      int cx = w / 2;
      int cy = h / 2;
      (cx, cy);// Move the screen and place the pointer in the center      if (mValues != null) {
        (-mValues[0]);
      }
      (mPath, mPaint);
    }
  }
}

For more information about Android related content, please check out the topic of this site:Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Android development introduction and advanced tutorial》、《Android programming activity operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.