SoFunction
Updated on 2025-04-07

Android programming development multitouch implementation method

This article describes the multitouch implementation method of Android programming development. Share it for your reference, as follows:

If you are interested in developing multi-touch programs, then this article will be a good start. In Android application development, multi-touch is not so out of reach and it is very simple to implement. In this example, only two classes are needed to implement multi-touch.

First, let’s take a look at our view class:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MTView extends SurfaceView implements  {
  private static final int MAX_TOUCHPOINTS = 10;
  private static final String START_TEXT = "Please touch the screen for testing";
  private Paint textPaint = new Paint();
  private Paint touchPaints[] = new Paint[MAX_TOUCHPOINTS];
  private int colors[] = new int[MAX_TOUCHPOINTS];
  private int width, height;
  private float scale = 1.0f;
  public MTView(Context context) {
    super(context);
    SurfaceHolder holder = getHolder();
    (this);
    setFocusable(true); // Make sure our View gets input focus    setFocusableInTouchMode(true); // Make sure to receive touch screen events    init();
  }
  private void init() {
    // Initialize 10 different colors of brushes    ();
    colors[0] = ;
    colors[1] = ;
    colors[2] = ;
    colors[3] = ;
    colors[4] = ;
    colors[5] = ;
    colors[6] = ;
    colors[7] = ;
    colors[8] = ;
    colors[9] = ;
    for (int i = 0; i < MAX_TOUCHPOINTS; i++) {
      touchPaints[i] = new Paint();
      touchPaints[i].setColor(colors[i]);
    }
  }
  /*
    * Handle touch screen events
    */
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // Get the number of screen contacts    int pointerCount = ();
    if (pointerCount > MAX_TOUCHPOINTS) {
      pointerCount = MAX_TOUCHPOINTS;
    }
    // Lock Canvas and start the corresponding interface processing    Canvas c = getHolder().lockCanvas();
    if (c != null) {
      ();
      if (() == MotionEvent.ACTION_UP) {
        // Clear the screen when your hand leaves the screen      } else {
        // First draw a cross on the screen, then draw a circle        for (int i = 0; i < pointerCount; i++) {
          // Get the coordinates of a contact and start drawing          int id = (i);
          int x = (int) (i);
          int y = (int) (i);
          drawCrosshairsAndText(x, y, touchPaints[id], i, id, c);
        }
        for (int i = 0; i < pointerCount; i++) {
          int id = (i);
          int x = (int) (i);
          int y = (int) (i);
          drawCircle(x, y, touchPaints[id], c);
        }
      }
      // After drawing, unlock      getHolder().unlockCanvasAndPost(c);
    }
    return true;
  }
  /**
    * Draw cross and coordinate information
    *
    * @param x
    * @param y
    * @param paint
    * @param ptr
    * @param id
    * @param c
    */
  private void drawCrosshairsAndText(int x, int y, Paint paint, int ptr,
      int id, Canvas c) {
    (0, y, width, y, paint);
    (x, 0, x, height, paint);
    int textY = (int) ((15 + 20 * ptr) * scale);
    ("x" + ptr + "=" + x, 10 * scale, textY, textPaint);
    ("y" + ptr + "=" + y, 70 * scale, textY, textPaint);
    ("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint);
  }
  /**
    * Draw a circle
    *
    * @param x
    * @param y
    * @param paint
    * @param c
    */
  private void drawCircle(int x, int y, Paint paint, Canvas c) {
    (x, y, 40 * scale, paint);
  }
  /*
    * When entering the program, draw the background black, and then write "START_TEXT" to the screen
    */
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
     = width;
     = height;
    if (width > height) {
       = width / 480f;
    } else {
       = height / 480f;
    }
    (14 * scale);
    Canvas c = getHolder().lockCanvas();
    if (c != null) {
      // Black background      ();
      float tWidth = (START_TEXT);
      (START_TEXT, width / 2 - tWidth / 2, height / 2,
          textPaint);
      getHolder().unlockCanvasAndPost(c);
    }
  }
  public void surfaceCreated(SurfaceHolder holder) {
  }
  public void surfaceDestroyed(SurfaceHolder holder) {
  }
}

The corresponding comments have been made in the code, so I won't talk about it here.

Next, take a look at our Activity.

package ;
import ;
import ;
import ;
import ;
public class MultitouchVisible extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    //Hide the title bar    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Set to full screen    getWindow().setFlags(.FLAG_FULLSCREEN,
        .FLAG_FULLSCREEN);
    //Set as MTView above    setContentView(new MTView(this));
  }
}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android programming activity operation skills summary》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android resource operation skills summary"and"Android control usage summary

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