Search code examples
androidzoomingmotionevent

Trying to implement 2 finger zoom


I am able to pan with my application but I am trying to implement a two finger pinch zoom. I am creating a map quiz with a large bitmap. I haven't found a simple way to do this - any help would be greatly appreciated. Thanks! Here is my code:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class MainActivity extends Activity {

private float mx, my;
private float curX, curY;

private ScrollView vScroll;
private HorizontalScrollView hScroll;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz);

    vScroll = (ScrollView) findViewById(R.id.vScroll);
    hScroll = (HorizontalScrollView) findViewById(R.id.hScroll);



}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float curX, curY;

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            mx = event.getX();
            my = event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            curX = event.getX();
            curY = event.getY();
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            mx = curX;
            my = curY;
            break;
        case MotionEvent.ACTION_UP:
            curX = event.getX();
            curY = event.getY();
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            break;


    }

    return true;
}

}

Solution

  • A lot of documentation you can find on http://developer.android.com/training/gestures/index.html.

    About scaling http://developer.android.com/training/gestures/scale.html

    Here exactly what you need http://developer.android.com/training/gestures/scale.html#scale