About Android multi-touch

Source: Internet
Author: User

Recently, a multi-touch scaling function is required. Then I checked the information online and summarized it as follows:

First, the android SDK version is very important. For example, if you specify Android: minsdkversion = "4" in androidmanifest. xml and select Android 2.2 when creating the project, your application is compatible with android1.6 ~ Android2.2

. However, the multi-touch API is 1.6 ~~ 2, 1 ~~, 2, 2 ~~ The three versions are different. For example, ontouchevent (motionevent event) in android2.2 can be used here. getactionmasked ()

Used for multi-touch detection. However, the event. getactionmasked () method is not found in 1.6 and 2.1. After checking the source code, it is found that the event. getaction () & motionevent. action_mask encapsulates it.

We should pay special attention to these small details, because it is related to version compatibility issues, maybe you have no problem running on the 2.2 device, but 1.6 or 2.1 may have a problem. Therefore, pay special attention to API

. 1.5.

Next, let's look at the Code:

1 package laahaa. Framework;
2
3
4
5 import Android. content. context;
6 Import Android. Graphics. pointf;
7 Import Android. util. attributeset;
8 Import Android. util. floatmath;
9 Import Android. View. motionevent;
10 Import Android. View. surfaceholder;
11
12 /**
13 *
14 * game canvas
15 */
16 public class gameview2x extends gameview implements surfaceholder. Callback {
17
18 private float olddist;
19 private pointf midpoint = new pointf ();
20 private Boolean iszoom = false;
21
22 public gameview2x (context, attributeset attrs ){
23 super (context, attrs );
24
25}
26
27 public Boolean ontouchevent (motionevent event ){
28
29 switch (event. getaction () & motionevent. action_mask ){
30 case motionevent. action_down:
31 super. actiondown (event );
32 break;
33 case motionevent. action_pointer_up:
34 iszoom = false;
35 break;
36 /**
37 * The original API is a non-primary pointer has gone down.
38 * Translation: not the first point press
39 */
40 case motionevent. action_pointer_down:
41 olddist = spacing (event );
42. midpoint (midpoint, event );
43 iszoom = true;
44 break;
45 case motionevent. action_move:
46 If (iszoom ){
47 float newdist = spacing (event );
48 /**
49 * indicates that the distance between the two fingers is greater than that between the two fingers.
50 .)
51 */
52 If (newdist + 10> olddist ){
53 super. getgamethread (). getgamedraw ()
54. checkxy (INT) midpoint. X, (INT) midpoint. y );
55 super. getgamethread (). getgamedraw (). setiszoom (true );
56}
57 /**
58 * indicates that the distance between the two fingers is smaller than that between the two fingers.
59 */
60 if (newdist + 10 <olddist ){
61 super. getgamethread (). getgamedraw (). setiszoom (false );
62 gamedraw. newx = 0;
63 gamedraw. newy = 0;
64}
65}
66 super. actionmove (event );
67
68 break;
69}
70
71 return true;
72}
73
74 private float spacing (motionevent event ){
75 float x = event. getx (0)-event. getx (1 );
76 float y = event. Gety (0)-event. Gety (1 );
77 return floatmath. SQRT (x * x + y * y );
78}
79
80 private void midpoint (pointf point, motionevent event ){
81 float x = event. getx (0) + event. getx (1 );
82 float y = event. Gety (0) + event. Gety (1 );
83 point. Set (X/2, Y/2 );
84}
85}

Note that gameview2x indicates that the version above 2.0 inherits from the earlier version of gameview I wrote. Because there are a lot of repeated code inheritance is required. Note the following: Get the SDK version code of the current application:

Int sdkversion = integer. parseint (build. version. SDK );

You need to run it at the beginning of initialization. It is better to write it into gameconfig, and it will be better to call it in a single example later. Or you do not like the direct static final of a single instance. Then, when selecting gameview, add an if judgment,

If (sdkversion <build. version_codes.eclair)

{}

Else

{}

Well, the multi-touch zoom here is relatively simple. In fact, you can dynamically set the zoom based on the ratio of the front distance to the back distance. This is just a simple example.

PS: The motionevent parameter in the ontouchevent event was recently found. It has a getsize () method. At one point, this method returns 0 forever, while at two electric shocks, this method returns different values based on the relative position of the two points

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.