Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It depends on the version of Android you wish to target.</p> <ul> <li>2.2 or newer - use the built-in ScaleGestureDetector. The javadoc is very helpful, but see the example below.</li> <li>2.0-2.2 - ScaleGestureDetector isn't built-in, so <a href="http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/view/ScaleGestureDetector.java" rel="nofollow">copy the version from Android</a> and compile it into your application.</li> <li>Pre-2.0 - Mutitouch wasn't supported before 2.0, so you need to <a href="http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/view/ScaleGestureDetector.java" rel="nofollow">copy ScaleGestureDetector.java from Android</a> and do a little more work to not use any multitouch APIs on unsupported devices:</li> </ul> <p>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 <code>ScaleGestureDetectorInterface</code> and our dummy implementation <code>FakeScaleGestureDetector</code>.</p> <p>Here is a sample supporting pre-2.0 devices:</p> <pre><code>// 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 &lt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload