使用捏缩放webview进行滑动

时间:2014-10-14 06:48:37

标签: android webview swipe gesture-recognition pinchzoom

我想添加内置缩放控件的滑动手势。 我正在使用

https://github.com/championswimmer/SimpleFingerGestures_Android_Library

用于滑动手势的库。这很简单

这是我的webview代码:

webcore = (WebView) findViewById(R.id.webcore);
    webcore.getSettings().setJavaScriptEnabled(true);
    webcore.setWebViewClient(new CustomWebViewClient());
    webcore.getSettings().setAppCacheEnabled(true);
    webcore.getSettings().setBuiltInZoomControls(true);

webcore.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            return mySfg.onTouch(arg0, arg1) || webcore.onTouchEvent(arg1);

        }
    });

//和我的听众代码

mySfg.setOnFingerGestureListener(new SimpleFingerGestures.OnFingerGestureListener() {

        @Override
        public boolean onSwipeUp(int fingers, long gestureDuration) {

            return false;
        }

        @Override
        public boolean onSwipeDown(int fingers, long gestureDuration) {

            return false;
        }

        @Override
        public boolean onSwipeLeft(int fingers, long gestureDuration) {
            move="left";
            if (webcore.canGoForward() && gestureDuration >= 500) {
                webcore.goForward();
            }
            return false;
        }

        @Override
        public boolean onSwipeRight(int fingers, long gestureDuration) {
        move="right";
            if (webcore.canGoBack() && gestureDuration >= 500) {
                webcore.goBack();
            }
            return false;
        }

        @Override
        public boolean onPinch(int fingers, long gestureDuration) {

            return true;
        }

        @Override
        public boolean onUnpinch(int fingers, long gestureDuration) {

            return true;
        }
    });

我很困惑我在这里做错了什么。任何帮助将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:0)

我通过实现自己的自定义webview并设置了builtInZoom设置来解决了这个问题。

以下是我使用此方法的自定义网页视图:

/**
 * Disable the controls
 */
private void disableControls(){
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        // Use the API 11+ calls to disable the controls
        this.getSettings().setBuiltInZoomControls(true);
        this.getSettings().setDisplayZoomControls(false);
    } else {
        // Use the reflection magic to make it work on earlier APIs
        getControlls();
    }
}

/**
 * This is where the magic happens :D
 */
private void getControlls() {
    try {
        Class webview = Class.forName("android.webkit.WebView");
        Method method = webview.getMethod("getZoomButtonsController");
        zoom_controll = (ZoomButtonsController) method.invoke(this, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);
    if (zoom_controll != null){
        // Hide the controlls AFTER they where made visible by the default implementation.
        zoom_controll.setVisible(false);
    }
    return true;
}