Analysis of realization method of multi-point touch in Android development _android

Source: Internet
Author: User
Tags gety touch

Multi-point touch technology in the actual development process, the most used is to enlarge and reduce the function. For example, there are some image browsers, you can use more than one finger on the screen to operate, to enlarge or shrink the picture. For example, some browsers can also enlarge or shrink the font by multi-touch. In fact, amplification is just a multi-touch practical application of one of the examples, with multi-touch technology, to a certain extent, we can innovate more ways to operate, to achieve a more cool human-computer interaction.

In theory, the Android system itself can handle up to 256 fingers, depending largely on the support of the phone's hardware. Of course, support Multi-Touch phone, also will not support so many points, generally support 2 or 4 points. For developers, there is no big difference between writing multi-touch code and writing code for a single point of touch. This is because the Motionevent class in the Android SDK not only encapsulates a single touch message, but also encapsulates a multi-touch message, which is almost the same for a single touch and multi-touch approach.

In processing a single point of touch, we typically use Motionevent.action_down, action_up, and Action_move, and can then be processed separately using a switch statement. Action_down and Action_up is a single touch screen, press down and open the operation, Action_move is the finger on the screen to move the operation.

In the process of dealing with multi-touch, we also need to use Motionevent.action_mask. Switch (event.getaction () & Motionevent.action_mask) is generally used to handle Action_pointer_down and ACTION_POINTER_UP events that handle multi-touch. After the code calls this "and" operation, when the second finger is pressed or released, the Action_pointer_down or Action_pointer_up event is triggered.

Let's use a practical example to illustrate how to implement Multi-Touch in your code. Here we load a picture, loading the picture, you can through a finger to drag the picture, or through the two fingers sliding to achieve the image zoom in and out function.

Copy Code code as follows:

Import slightly
public class Imagevieweractivity extends activity implements Ontouchlistener {

Private ImageView Mimageview;

Private Matrix matrix = new Matrix ();
Private Matrix Savedmatrix = new Matrix ();

private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;

The first one to press the point of the finger
Private PointF startpoint = new PointF ();
The midpoint of the touch point of the two pressed fingers
Private PointF midpoint = new PointF ();
The initial two fingers pressed the distance of the touch point
Private float Oridis = 1f;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.setcontentview (R.layout.imageviewer);
Mimageview = (ImageView) This.findviewbyid (R.id.imageview);
Mimageview.setontouchlistener (this);
}

@Override
public boolean Ontouch (View V, motionevent event) {
ImageView view = (ImageView) v;

Conduct and operation is to judge the multi-touch.
Switch (event.getaction () & Motionevent.action_mask) {
Case Motionevent.action_down:
The first finger presses the event
Matrix.set (View.getimagematrix ());
Savedmatrix.set (matrix);
Startpoint.set (Event.getx (), event.gety ());
mode = DRAG;
Break
Case Motionevent.action_pointer_down:
The second finger presses the event
Oridis = Distance (event);
if (Oridis > 10f) {
Savedmatrix.set (matrix);
Midpoint = middle (event);
mode = ZOOM;
}
Break
Case MOTIONEVENT.ACTION_UP:
Case MOTIONEVENT.ACTION_POINTER_UP:
Finger Release Event
mode = NONE;
Break
Case Motionevent.action_move:
Finger Slip Event
if (mode = = DRAG) {
is a finger to drag
Matrix.set (Savedmatrix);
Matrix.posttranslate (Event.getx ()-Startpoint.x, Event.gety ()
-STARTPOINT.Y);
else if (mode = = ZOOM) {
Two fingers sliding
float newdist = distance (event);
if (Newdist > 10f) {
Matrix.set (Savedmatrix);
float scale = Newdist/oridis;
Matrix.postscale (scale, scale, midpoint.x, MIDPOINT.Y);
}
}
Break
}

Set the matrix for ImageView
View.setimagematrix (matrix);
return true;
}

Calculate the distance between two touch points
Private float distance (Motionevent event) {
float x = event.getx (0)-event.getx (1);
Float y = event.gety (0)-event.gety (1);
return FLOATMATH.SQRT (x * x + y * y);
}

Calculates the midpoint of two touch points
Private PointF Middle (motionevent event) {
float x = event.getx (0) + event.getx (1);
Float y = event.gety (0) + event.gety (1);
return new PointF (X/2, Y/2);
}

}

The following is a layout file.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<imageview
Android:id= "@+id/imageview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:src= "@drawable/example"
Android:scaletype= "Matrix" >
</ImageView>
</RelativeLayout>


In this code, we compute the matrix value by manipulating the finger, and then set the matrix of the picture to move and scale the picture.

It should be noted that in the resource file, you need to set the ImageView ScaleType as "Matrix".

Experience Sharing:

Generally speaking, the handset's screen is small, handles 2 fingers to be sufficient, puts on 3 and above finger operation, is a little difficult. So in the general design process, the realization of 2 fingers is enough.

Many mobile phones do not support multi-touch, so there must be other ways to achieve the desired functionality. For example, the above picture scaling example, in the actual product development, it is important to design a regular way to achieve the image scaling, such as using a button, but not completely dependent on multi-touch.

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.