Android Droid查询如何识别捏合和缩放

时间:2013-08-22 10:36:30

标签: android android-gesture droidquery

我一直在使用droidQuery库,它非常好用且易于使用。到目前为止,我已经将它用于自定义手势检测。我有一个swipeInterceptorView,带有用于检测滑动的滑动侦听器,但我还需要在我的应用中检测捏合和缩放。谢谢!

1 个答案:

答案 0 :(得分:2)

droidQuery库目前不支持缩放缩放,但您可以使用 droidQuery Android ScaleGestureDetector来滑动缩放功能得到你正在寻找的手势检测。

以下示例应该可以帮助您:

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;
        }
    }
}