[Android] multi-point touch in Android Development

Source: Internet
Author: User
Tags gety

Multitouch is a technology that allows a computer user to control the graphic interface through multiple fingers at the same time. Compared with the multi-touch technology, the single-touch device has been in touch for many years, and Small-sized mobile phones have been in touch, the most common big-sized ATM machines and queuing query machines in banks.

In actual development, the most commonly used multi-touch technology is the zoom-in and zoom-out function. For example, some image browsers can use multiple fingers to zoom in or out the images on the screen. For example, Some browsers can also zoom in or out the font by multi-touch. In fact, amplification and reduction are only one of the practical application examples of multi-touch. With the multi-touch technology, more operations can be innovated to a certain extent, achieve cool human-computer interaction.

In theory, the android system can handle the touch of up to 256 fingers, depending on the support of mobile phone hardware. Of course, mobile phones that support multi-touch operations do not support such multi-point operations. Generally, they support two or four points. For developers, writing multi-touch code is not very different from writing single-touch code. This is because the motionevent class in Android SDK not only encapsulates single-touch messages, but also multi-touch messages. The processing methods for single-touch and multi-touch messages are almost the same.

In processing spof, we generally use motionevent. action_down, action_up, and action_move. Then we can use a switch statement to process them separately. Action_down and action_up are single-point touch screens. The following operations are performed to move your fingers on the screen.

In the process of processing multi-point touch, we also need to use motionevent. action_mask. Generally, you can use switch (event. getaction () & motionevent. action_mask) to process the action_pointer_down and action_pointer_up events of multi-point touch. After the Code calls the "and" operation, when the second finger is pressed or opened, the action_pointer_down or action_pointer_up event is triggered.

The following example shows how to implement the multi-touch function in code. Here we load an image. After loading the image, we can drag the image with one finger or slide the two fingers to enlarge and reduce the image.

// Import slightly public class imagevieweractivity extends activity implements ontouchlistener {private imageview mimageview; private 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 point of the first pressed finger is private pointf startpoint = new pointf (); // The point of the touch point of the two pressed fingers is private pointf midpoint = new pointf (); // The initial distance between the touch points pressed by two fingers is private float oridis = 1f; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. imageviewer); mimageview = (imageview) This. findviewbyid (R. id. imageview); mimageview. setontouchlistener (this) ;}@ overridepublic Boolean ontouch (view V, motionevent event) {imageview view = (imageview) V; // you can perform and operate on multiple-point touch switches (events. 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 press 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: // click it to open the event mode = none; break; Case motionevent. action_move: // If (mode = drag) for a finger slide event {// It is a finger dragging matrix. set (savedmatrix); matrix. posttranslate (event. getx ()-startpoint. x, event. gety ()-startpoint. y);} else if (mode = zoom) {// two fingers slide float newdist = distance (event); If (newdist> 10f) {matrix. set (savedmatrix); float scale = newdist/oridis; matrix. postscale (scale, scale, midpoint. x, midpoint. y) ;}} break;} // sets the matrixview of the imageview. 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);} // calculate the point of the 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 are layout files.

<?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 use finger operations to calculate the matrix value, and then set the matrix of the image to move and scale the image.

Note that in the resource file, set the scaletype of imageview to "matrix ".

 

Experience Sharing:

Generally speaking, the screen of the mobile phone is small, and it is enough to handle 2 fingers. It is a little difficult to put 3 or more fingers on it. Therefore, in the general design process, it is enough to implement two fingers.

Many mobile phones do not support multi-touch, so you must have other methods to implement the required functions. For example, in the above image scaling example, in actual product development, we must design a conventional method to achieve image scaling, such as using buttons, rather than relying entirely on multi-point touch.

 

---------------------------------------------------------------------------

Http://blog.csdn.net/arui319

An excellent solution for Android Application Development has been published. This article is part of the first draft. Welcome to purchase and read.

This article can be reproduced, but please keep the above author information.

Thank you.

---------------------------------------------------------------------------

 

 

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.