Android: A two-touch case

Source: Internet
Author: User
Tags gety

The following is a two-touch case code:

package com.zzj;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;public class AndroidTestActivity extends Activity {    private float x0, y0;    private float x1, y1;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    public boolean onTouchEvent(MotionEvent event) {        int pointerCount = event.getPointerCount();        int action = event.getAction();        if (pointerCount == 1) {            switch (action) {            case MotionEvent.ACTION_DOWN:                x0 = event.getX(0);                y0 = event.getY(0);                System.out.println("ACTION_DOWN pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_UP:                System.out.println("ACTION_UP pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_MOVE:                System.out.println("ACTION_MOVE pointerCount=" + pointerCount);                break;            }        }        if (pointerCount == 2) {            switch (action) {            case MotionEvent.ACTION_DOWN:                x0 = event.getX(0);                y0 = event.getY(0);                System.out.println("ACTION_DOWN pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_UP:                System.out.println("ACTION_UP pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_MOVE:                System.out.println("ACTION_MOVE pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_POINTER_1_DOWN:                System.out.println("ACTION_POINTER_1_DOWN pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_POINTER_1_UP:                System.out.println("ACTION_POINTER_1_UP pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_POINTER_2_DOWN:                System.out.println("ACTION_POINTER_2_DOWN pointerCount=" + pointerCount);                break;            case MotionEvent.ACTION_POINTER_2_UP:                System.out.println("ACTION_POINTER_2_UP pointerCount=" + pointerCount);                break;            }        }        return super.onTouchEvent(event);    }}

The following are some key points of this case: 1) Use event. getPointerCount () to get the number of current touch points. And determine the touch points to process different points separately.

2) use event. getAction () is used to obtain the current event code. The events of single-point press, release, and movement are: MotionEvent. ACTION_DOWN, ACTION_UP, and ACTION_MOVE; the events for pressing, releasing, and moving the second vertex are ACTION_POINTER_2_DOWN, ACTION_POINTER_2_UP, and ACTION_MOVE. Note that the single point and two points respond to the same moving event, that is, ACTION_MOVE.

3) MotionEvent. action_pointer_closing down and MotionEvent. action_pointer_closing up will be returned only after the second point is pressed. The first click will not respond to these two event codes. Remember this.

4) use event. getX (0), event. getY (0) to obtain the coordinate value of the first vertex, through event. getX (1), event. getY (1) to obtain the coordinate value of the second vertex. If there are more points, and so on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.