android pinch zoom
Asked Answered
Y

7

49

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

Yamen answered 21/3, 2011 at 9:33 Comment(0)
W
43

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

Whore answered 21/3, 2011 at 10:4 Comment(2)
This answer will be worthless, once the links go down. At least quote the relevant parts.Hindquarter
FYI, this answer is out-of-date. ScaleGestureDetector is Android's answer for pinch-zoom, since API 8. See Emanuel's answer.Sekofski
M
38

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() {
    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
    }
    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        Log.d(LOG_KEY, "zoom ongoing, scale: " + detector.getScaleFactor());
        return false;
    }
});

You override onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {
    mScaleDetector.onTouchEvent(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().

Monagan answered 30/7, 2012 at 13:45 Comment(1)
Additional information in Android docs - Dragging and Scaling Especially note example code showing how to combine panning (scrolling) with scaling (pinch zoom).Sekofski
G
16

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;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            TextView textView = (TextView) findViewById(R.id.text);

            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" );
                    }
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
                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);
                            }
                        }
                    }
                    break;
                }
            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).

Garble answered 21/3, 2011 at 10:23 Comment(6)
Can i know your MyGestureDetector() classNaos
@Renanlf That was more than a year ago. Sorry, I don't even code for android anymore. Nor do I have those sources anywhere.Garble
MyGestureDetector() is not relevant here; because of the declaration GestureDetector(GestureDetector.OnGestureListener listener) we conclude that it's a misnomer child of GestureDetector.OnGestureListener. And the events detected by that class are irrelevant for pinch zooming.Vicissitude
switch (event.getAction() & MotionEvent.ACTION_MASK), in this line of the code: ACTION_MASK turns 255 (ff) so what is the point of adding Motion.ACTION_MASK? Will switch (event.getAction()) be enough, if not why? Could you please explain it.Solorio
@folone Could you help me in this question. This question is similar to what I am facingTampere
The code does not seeem to work. MyGestureDetector is not defined, and so are other things in the code, like spacing, TAG, ...)Precedent
F
5

There is also this project that does the job and worked perfectly for me: https://github.com/chrisbanes/PhotoView

Furiya answered 25/2, 2013 at 15:52 Comment(0)
P
4

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
Pavier answered 24/3, 2011 at 10:20 Comment(1)
link only answers get worthless the the links get broken. Try adding a few main key points to the answer independent of the linksAparicio
M
1

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

Available here

Meaghanmeagher answered 10/3, 2012 at 1:59 Comment(0)
L
0

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: https://bitbucket.org/warwick/hacergestov3 Or you can download the demo app here: https://play.google.com/store/apps/details?id=com.WarwickWestonWright.HacerGestoV3Demo This is a pure canvas library so it can be used in pretty any scenario. Hope this helps.

Liebfraumilch answered 24/4, 2018 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.