Question

I have been using the droidQuery library, which is really great and easy to use. I have used it for custom gesture detection mostly so far. I have a swipeInterceptorView with a swipe listener for detecting swipes, but I also need to detect pinching and zooming in my app. Thanks!

Was it helpful?

Solution

The droidQuery library does not currently support pinch zooming, however you can use droidQuery's swiping ability with Android's ScaleGestureDetector to get the gesture detection you are seeking.

The following example, should help you:

public class MyActivity extends Activity {

    private float scaleFactor = 1.0f;
    private ScaleGestureDetector scaleGestureDetector;
    private SwipeInterceptorView view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set main view to the main layout
        setContentView(R.layout.main);
        //get a reference to the content view
        view = (SwipeInterceptorView) findViewById(R.id.swipe_view);
        //get the scale gesture detector
        scaleGestureDetector = new ScaleGestureDetector(context, new ScaleListener());
        //add Swiper
        view.setSwipeListener(new SwipeListener() {
            public void onUpSwipe(View v) {
                //TODO handle up swipe
            }
            public void onRightSwipe(View v) {
                //TODO handle right swipe
            }
            public void onLeftSwipe(View v) {
                //TODO handle left swipe
            }
            public void onDownSwipe(View v) {
                //TODO handle down swipe
            }
        });
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (scaleGestureDetector.onTouchEvent(event))//this will cause a pinch zoom if two fingers are down
                    return true;
                //if no pinch zoom was handled, the swiping gesture will take over.
                return super.onTouch(v, event);
            }
        });
    }

    //this inner class copied from http://www.vogella.com/articles/AndroidTouch/article.html#scaleGestureDetector
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f));

            view.invalidate();
            return true;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top