Basic knowledge of multi-touch and gesture in Android

Source: Internet
Author: User
Tags gety

Currently, capacitive touch screens are generally used for android phones. Therefore, multi-touch is basically supported. In android Systems, Applications can also use multi-touch events to fulfill various gestures and scenarios, the following describes how to use multi-touch:

1. Related interfaces and events

MotionEvent.getAction() 

For single-touch, we can get the following events through MotionEvent. getAction (): ACTION_DOWN, ACTION_UP, etc.

MotionEvent.ACTION_MASK

For multi-touch, we need to get the following results from MotionEvent. getAction () & MotionEvent. ACTION_MASK: ACTION_POINTER_DOWN, ACTION_POINTER_UP, etc.

Single point and multi-point share: ACTION_MOVE and so on, so you need to distinguish whether the current single point or multi-point can be correctly processed.

These obtained values are constants in MotionEvent and can be called directly.

There are also many auxiliary interfaces:

Event. getPointerCount () gets the number of current touch points event. getPointerId (I) is used to obtain the Id of a specified touch point, which is used to differentiate different touch point events. getX (I) obtains the X coordinate event of the specified touch point. getY (I) obtains the Y coordinate event of the specified touch point. getPressure (I) is used to obtain the pressure value of a specified touch point. This value must be supported by the LCD hardware and driver.


2. Commonly Used gesture processing-Dual-finger Scaling

The dual-finger scaling gesture is often used to zoom in or out images, texts, webpages, and so on. In practice, it is frequently used. The following describes the specific principles:

If there is no other gesture interference at present, we only need to detect the respective positions when two fingers are pressed, the distance between two points, and the distance between the two fingers following the movement, and the relationship between their respective positions and highlights, if the distance increases after moving, the zoom action is used; otherwise, the zoom action is used.

First, calculate the distance between the two fingers:

We can get the distance between the two vertices before moving. I believe that the students who have learned the hook theorem should know the specific principle, the length of a straight corner edge can be obtained from x of point 1 minus x of point 2. Positive and Negative are not important, because the square is calculated and the positive length is obtained in the following calculation, then, the length of the other right corner edge can be obtained from y of vertex 1 minus y of vertex 2, so that the length of the Oblique Edge can be obtained from the two right corner sides by using the stock theorem, that is, the linear distance between two points.

   private float spacing(MotionEvent event)   {        float x = event.getX(0)-event.getY(1);        float y = event.getY(0)-event.getY(1);        return (float)Math.sqrt(x*x+y*y);    }

At the same time, we also need to calculate the midpoint of the two fingers to serve as the center benchmark of scaling. This calculation method is simpler, add x of point 2 to x of point 1 and divide it by 2 to obtain the x coordinate of the midpoint. Add y of point 1 to y of point 2 and divide it by 2 to obtain the y coordinate of the midpoint, finally, the coordinates of the midpoint are obtained:

    private void midPoint(PointF point, MotionEvent event)    {          float x = event.getX(0) + event.getX(1);          float y = event.getY(0) + event.getY(1);          point.set(x / 2, y / 2);     }

Then, in the ACTION_MOVE event processing, the straight line distance between the moving highlights is obtained. The calculation method is the same as above. If the moving distance is greater than the pressing distance, the amplification is performed, otherwise, the scale-down is performed. The ratio of Scale-in and scale-out is calculated from the distance before the new distance or move, then, you can use the relevant interfaces and operations to zoom in or out. Common scaling methods:

Use postscale of Martix (horizontal scaling ratio, vertical scaling ratio, x of the midpoint, y of the midpoint). Generally, You need to obtain the martix of an image or control first, then, call postscale to pass the parameter to get the new scaled martix, and then set it back. After the refresh, it will take effect to see the scaled effect.



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.