10

I just wanted to know if there is some kind of class to use the multitouch features of android 2.1. Specifically I am trying to implement pinch-zoom and was wondering if I always have to measure the distance between two touch events and calculate the zoom level on my own?

Thanks, chris

Mark B
  • 183,023
  • 24
  • 297
  • 295
krise
  • 101
  • 1
  • 1
  • 3
  • 1
    I'm just starting out on the android dev stuff myself, if there isn't one, this would be a great opportunity to start one and make it available to others. – onaclov2000 Apr 15 '10 at 13:30
  • You seem to be confusing the term "multitouch". What you are asking for specifically is "pinch zoom" functionality. Which is one of the things that can be accomplished using multitouch. Multitouch functionality is simply the ability to detect two or more touch events on the screen at the same time. – Mark B Apr 15 '10 at 14:02

5 Answers5

15

I'm trying to do the same thing, and as usual my first instinct was to look into the Android source code itself. The interesting bits seem to be in class ScaleGestureDetector, which is not public but its javadoc says

@hide Pending API approval

so hopefully it will become public at some point.

Update: ScaleGestureDetector is now part of the Android 2.2 API.

Mirko N.
  • 10,537
  • 6
  • 38
  • 37
4

I believe you will need to calculate the zoom level yourself. This article looks like a good resource to get you started: http://blogs.zdnet.com/Burnette/?p=1847

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 1
    I think Ed Burnette (author of that post) goes into greater detail in his upcoming third edition of his book, Hello, Android. – CommonsWare Apr 15 '10 at 14:06
3

It depends on the version of Android you wish to target.

  • 2.2 or newer - use the built-in ScaleGestureDetector. The javadoc is very helpful, but see the example below.
  • 2.0-2.2 - ScaleGestureDetector isn't built-in, so copy the version from Android and compile it into your application.
  • Pre-2.0 - Mutitouch wasn't supported before 2.0, so you need to copy ScaleGestureDetector.java from Android and do a little more work to not use any multitouch APIs on unsupported devices:

To avoid using multitouch APIs on pre-2.0 devices, you need to create an interface for the ScaleGestureDetector (Eclipse can do this via the Refactor menu), and a dummy implementation which 1.x devices will use. We'll call our interface ScaleGestureDetectorInterface and our dummy implementation FakeScaleGestureDetector.

Here is a sample supporting pre-2.0 devices:

// If you don't care about pre-2.0 devices, just make this a
// ScaleGestureDetector and skip the API check in the constructor.
private final ScaleGestureDetectorInterface mScaleDetector;

public MyView {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ECLAIR) {
        // Use the fake version which won't call any multitouch APIs
        mScaleDetector = new FakeScaleGestureDetector();
    } else {
        // We are using SDK 2.0+, use the real implementation.
        mScaleDetector = new ScaleGestureDetector(context,
            new MyScaleListener());
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // On pre-2.0, the implementation does nothing.
    return mScaleDetector.onTouchEvent(event);
}

private class MyScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureInterface detector) {
        float scaleFactor = detector.getScaleFactor();
        // If you were using a matrix to zoom an ImageView, you would do
        // something like this:
        mMatrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(),
            detector.getFocusY());
        return true;
    }
}
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
Brad
  • 5,492
  • 23
  • 34
1

There have been hacks created by developers that enable multitouch on the browser and Dolphin browser. These come in custom roms and I am sure that they are downloadable.

Also Google has released multi-touch officially on the Nexus One and Motorola on their milestone. This means that you should be able to get an official class for it but I bet that its for version 2.1 of Android.

Also I think that it would be safe to assume that you want this to work on rooted phones. Than means that you may be stuck at using Android 2.1 and maybe all the way down to 2.0.

Jonathan Czitkovics
  • 1,642
  • 11
  • 11
  • The question is about Android development. This site is about software development. Your answer is for end users. – CommonsWare Apr 15 '10 at 14:03
  • @CommonsWare: This is very much about software development. If the API isn't there for Android 2.0 but is for Android 2.1, it does affect the outcome of his development. – Andrew Moore Apr 15 '10 at 18:49
0

Are you trying to zoom into a picture? As a simple solution, you could use a WebView to display your image, it has built-in pinch-zoom functionality.

molnarm
  • 9,856
  • 2
  • 42
  • 60