1

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!

superuser
  • 731
  • 10
  • 28

1 Answers1

2

My 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;
        }
    }
}
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Phil
  • 35,852
  • 23
  • 123
  • 164
  • the pinching and zooming are recognized as swiping – superuser Aug 24 '13 at 18:10
  • @superuser Is that instead of zooming, or with it? – Phil Aug 24 '13 at 18:17
  • it is instead of zooming. – superuser Aug 25 '13 at 01:00
  • I am adding the other stuff to the ontouch to look for zooming on textviews. Is that ok? – superuser Aug 25 '13 at 11:34
  • I have noticed that if I pinch or zoom, it is also recognized as a swipe. – superuser Aug 25 '13 at 13:19
  • @superuser, you may need to write a full custom `onTouchListener` to get exactly the functionality you seek. If you handle the touch action and action mask, you can determine the current number of fingers touching the screen, and work from there. [droidQuery] and [ScaleGestureDetecor] are meant as conveniences - but there may just be cases where neither suffice. – Phil Aug 25 '13 at 17:36
  • ok, thanks! I am writing some custom touch events so that it will never be handled as a swipe if two fingers had been down earlier. – superuser Aug 26 '13 at 11:58