android pinch zoom

The question:

My layout contains buttons, textviews, etc. Is it possible to implement pinch zoom in my layout?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Updated Answer

Code can be found here : official-doc

Answer Outdated

Check out the following links which may help you

Best examples are provided in the below links, which you can refactor to meet your requirements.

  1. Implementing the pinch zoom gesture

  2. Android-pinch

  3. GestureDetector.SimpleOnGestureListener

Method 2

For android 2.2+ (api level8), you can use ScaleGestureDetector.

you need a member:

private ScaleGestureDetector mScaleDetector;

in your constructor (or onCreate()) you add:

mScaleDetector = new ScaleGestureDetector(context, new OnScaleGestureListener() {
    public void onScaleEnd(ScaleGestureDetector detector) {
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    public boolean onScale(ScaleGestureDetector detector) {
        Log.d(LOG_KEY, "zoom ongoing, scale: " + detector.getScaleFactor());
        return false;

You override onTouchEvent:

public boolean onTouchEvent(MotionEvent event) {
    return true;

If you draw your View by hand, in the onScale() you probably do store the scale factor in a member, then call invalidate() and use the scale factor when drawing in your onDraw().
Otherwise you can directly modify font sizes or things like that in the onScale().

Method 3

I implemented a pinch zoom for my TextView, using this tutorial. The resulting code is this:

private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;

and in onCreate():

    // Zoom handlers
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {

        // We can be in one of these 2 states
        static final int NONE = 0;
        static final int ZOOM = 1;
        int mode = NONE;

        static final int MIN_FONT_SIZE = 10;
        static final int MAX_FONT_SIZE = 50;

        float oldDist = 1f;

        public boolean onTouch(View v, MotionEvent event) {
            TextView textView = (TextView) findViewById(;

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_POINTER_DOWN:
                    oldDist = spacing(event);
                    Log.d(TAG, "oldDist=" + oldDist);
                    if (oldDist > 10f) {
                       mode = ZOOM;
                       Log.d(TAG, "mode=ZOOM" );
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                case MotionEvent.ACTION_MOVE:
                    if (mode == ZOOM) {
                        float newDist = spacing(event);
                        // If you want to tweak font scaling, this is the place to go.
                        if (newDist > 10f) {
                            float scale = newDist / oldDist;

                            if (scale > 1) {
                                scale = 1.1f;
                            } else if (scale < 1) {
                                scale = 0.95f;

                            float currentSize = textView.getTextSize() * scale;
                            if ((currentSize < MAX_FONT_SIZE && currentSize > MIN_FONT_SIZE)
                                    ||(currentSize >= MAX_FONT_SIZE && scale < 1)
                                    || (currentSize <= MIN_FONT_SIZE && scale > 1)) {
                                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
            return false;

Magic constants 1.1 and 0.95 were chosen empirically (using scale variable for this purpose made my TextView behave kind of weird).

Method 4

There is also this project that does the job and worked perfectly for me:

Method 5

In android Honeycomb(API Level 11), it is possible, We can use setScaleX and setScaleY with pivot point
I have explained it here

  1. Zooming a view completely
  2. Pinch Zoom to view completely

Method 6

I have created a project for basic pinch-zoom that supports Android 2.1+

Available here

Method 7

I have an open source library that does this very well. It’s a four gesture library that comes with an out-of-the-box pan zoom setting. You can find it here:
Or you can download the demo app here:
This is a pure canvas library so it can be used in pretty any scenario.
Hope this helps.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment