SoFunction
Updated on 2025-03-10

Example of Android programming to implement two-point touch function

This article describes the implementation of two-point touch functions by Android programming. Share it for your reference, as follows:

Here is a two-point touch case code:

package ;
import ;
import ;
import ;
public class AndroidTestActivity extends Activity {
  private float x0, y0;
  private float x1, y1;
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
  }
  public boolean onTouchEvent(MotionEvent event) {
    int pointerCount = ();
    int action = ();
    if (pointerCount == 1) {
      switch (action) {
      case MotionEvent.ACTION_DOWN:
        x0 = (0);
        y0 = (0);
        ("ACTION_DOWN pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_UP:
        ("ACTION_UP pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_MOVE:
        ("ACTION_MOVE pointerCount=" + pointerCount);
        break;
      }
    }
    if (pointerCount == 2) {
      switch (action) {
      case MotionEvent.ACTION_DOWN:
        x0 = (0);
        y0 = (0);
        ("ACTION_DOWN pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_UP:
        ("ACTION_UP pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_MOVE:
        ("ACTION_MOVE pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_POINTER_1_DOWN:
        ("ACTION_POINTER_1_DOWN pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_POINTER_1_UP:
        ("ACTION_POINTER_1_UP pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_POINTER_2_DOWN:
        ("ACTION_POINTER_2_DOWN pointerCount=" + pointerCount);
        break;
      case MotionEvent.ACTION_POINTER_2_UP:
        ("ACTION_POINTER_2_UP pointerCount=" + pointerCount);
        break;
      }
    }
    return (event);
  }
}

Here are some analysis points of this case:

1) Use()To get the current number of touch points. And judge the touch points to handle events on different points respectively.

2) Use () to get the current event code. The events of single-click, release and move are:MotionEvent.ACTION_DOWNACTION_UPACTION_MOVE;The second click, release and move events areACTION_POINTER_2_DOWN、        ACTION_POINTER_2_UPACTION_MOVE
Note that single and two points respond to the same moving event, i.e.ACTION_MOVE

3) Only after the second click will it respondMotionEvent.ACTION_POINTER_1_DOWNMotionEvent.ACTION_POINTER_1_UP, the first click does not respond to these two event codes. Remember this.

4) Passed(0)(0)to get the coordinate value of the first point, by(1)(1)to get the coordinate value of the second point. If there are more points, and so on.

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 resource operation skills summary》、《Summary of Android data skills for operating json format》、《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.