About Android Multi-touch

Source: Internet
Author: User

Recent projects require a multi-touch zoom feature. And then went online to check the following information summary:

First of all, the Android SDK version is very important, such as you specify android:minsdkversion= "4" in the Androidmanifest.xml and the project is built when you choose Android 2.2 is to indicate that your app is compatible android1.6~ android2.2

Between the versions. But multi-touch APIs are different in three versions of 1.6~~2,1~~,2,2~~. For example, android2.2 ontouchevent (Motionevent event) can be used here event.getactionmasked ()

Represents a multi-touch detection point. But in 1.6 and 2.1 There is no event.getactionmasked () This method, check the source found that he is actually event.getaction () & Motionevent.action_mask encapsulated a bit.

We should pay special attention to these small details, because this is related to version compatibility issues, perhaps you are running on 2.2 of the device is no problem, but 1.6 or 2.1 will have a problem or something. So for the new features added after 1.6, pay special attention to the API

The use of. 1.5 will not be considered.

Next 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;
ten import Android.view.SurfaceHolder;
One
/**
*
14 * Game Canvas
* /
Public class GAMEVIEW2X extends Gameview implements Surfaceholder.callback {
-
private float olddist;
private PointF midpoint = new PointF ();
Private Boolean iszoom = false;
+
Public gameview2x (context context, AttributeSet attrs) {
the super (context, attrs);
-
+ }
-
Public Boolean ontouchevent (Motionevent event) {
-
switch (event.getaction () & Motionevent.action_mask) {
Case Motionevent.action_down:
Super.actiondown (event);
Break ;
Case MOTIONEVENT.ACTION_POINTER_UP:
iszoom = false;
Break ;
approx./**
the PNS * API is originally a non-primary pointer have gone down.
38 * Translation comes in: Not the first click
* *
Case Motionevent.action_pointer_down:
olddist = spacing (event);
Midpoint (midpoint, event);
iszoom = true;
A break ;
Case Motionevent.action_move:
if (iszoom) {
Float newdist = spacing (event);
/**
49 * Indicates that the new distance is greater than the distance two fingers have just touched.
50 * (+ 10 pixels to delay the magnification, or a little bit of pixels, also magnified, feeling too fast. )
Wuyi * *
if (newdist + > Olddist) {
Super.getgamethread (). Getgamedraw ()
checkxy ((int) midpoint.x, (int) midpoint.y);
Super.getgamethread (). Getgamedraw (). Setiszoom (true);
(+ $ }
/**
58 * Indicates that the new distance is smaller than the distance of two fingers just touched
* *
if (newdist + < olddist) {
Super.getgamethread (). Getgamedraw (). Setiszoom (false);
gamedraw.newx = 0;
gamedraw.newy = 0;
+ }
+ }
Super.actionmove (event);
the
;
A-I }
-
return true;
*
the
Spacing Private Float (motionevent event) {
• float x = event.getx (0)-event.getx (1);
Float y = event.gety (0)-event.gety (1);
floatmath.sqrt Return (x * x + y * y);
+ }
-
$ private void midpoint (PointF point, motionevent event) {
bayi Float x = event.getx (0) + event.getx (1);
event.gety (0) + event.gety (1);
Point.set (X/2, Y/2);
+ }
+ }


Note here that GAMEVIEW2X represents more than 2.0 of the version he inherited from the lower version of Gameview that I wrote myself. Because there are a lot of duplicate code inheritance is required. Finally, note the following: Get the SDK version code for the current app:

int Sdkversion=integer.parseint (Build.VERSION.SDK);

You need to run it at the very beginning of the initialization, and it's better to write to the Gameconfig, and the next single-instance call. Or you don't like the direct static final of a single case. And then choose Gameview when you add an if judgment,

if (sdkversion<build.version_codes. Eclair)

{}

Else

{}

OK, here the multi-touch zoom is relatively simple, in fact, according to the ratio of the front distance/back distance to dynamically set the zoom. This is just a simple example.

PS: Recently discovered parameter Motionevent in the Ontouchevent event, it has a getsize () method. At one point, the method always returns 0, while in two electric shocks, the method returns different values according to the relative position change of two points.

About Android Multi-touch

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.