6

我正在使用这段代码,它完全按照我的意愿工作。但是我必须在双击时实现另一个功能,并且想禁用双击缩放(但保留捏缩放功能)。

webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true); 
webView.setInitialScale(1);

试图手动计算比例,但没有运气(这感觉很复杂)。 在 web 视图上禁用双击缩放/取消缩放

Android Webview - 网页应适合设备屏幕

有没有办法使用 setUseWideViewPort 和 Zoomcontrolls 但只能禁用或覆盖双击缩放?

4

4 回答 4

12

最好的解决方案,只需从 MyWebView 扩展您的 WebView

 public class HelpWebView extends WebView {

    private GestureDetector gestureDetector;
    private AtomicBoolean mPreventAction = new AtomicBoolean(false);
    private AtomicLong mPreventActionTime = new AtomicLong(0);

    public HelpWebView(Context context) {
        super(context);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public HelpWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public HelpWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public HelpWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        int pointId = event.getPointerId(index);

        // just use one(first) finger, prevent double tap with two and more fingers
        if (pointId == 0){
            gestureDetector.onTouchEvent(event);

            if (mPreventAction.get()){
                if (System.currentTimeMillis() - mPreventActionTime.get() > ViewConfiguration.getDoubleTapTimeout()){
                    mPreventAction.set(false);
                } else {
                    return true;
                }
            }

            return super.onTouchEvent(event);
        } else {
            return true;
        }
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            mPreventAction.set(true);
            mPreventActionTime.set(System.currentTimeMillis());
            return true;
        }
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            mPreventAction.set(true);
            mPreventActionTime.set(System.currentTimeMillis());
            return true;
        }
    }
}
于 2013-03-21T12:42:36.040 回答
3

找到了解决方案:

class MyWebView extends WebView { 

    public boolean onTouchEvent(MotionEvent event) {

            gd.onTouchEvent(event);

            // disable double tap zooming

        if(doubleTap)
            {
                doubleTap = false;
                return false;
            }

            return super.onTouchEvent(event);
        }


    GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() { 




            public boolean onDoubleTap(MotionEvent e) {

                    showToast("Double tap");
                    doubleTap = true;

                    return false;
            }        
}
于 2012-04-26T11:36:08.983 回答
1

抱歉,我没有时间对此进行测试,但请尝试:

GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
    public boolean onDoubleTap(MotionEvent e) {
        showToast("Double tap");
        return true; //instead of false
    }
}
于 2012-04-26T09:50:36.927 回答
1

带有“一个(第一)手指”解决方案的 GestureDetector 无法可靠地工作。在 Galaxy S3 Android 4.0.4 上,Web 视图有时仍会放大。像这样的附加 WebViewClient 可用于在视图缩放时恢复缩放比例:

public class NoZoomedWebViewClient extends WebViewClient {
    private static final String LOG_TAG = "NoZoomedWebViewClient";
    private static final long STABLE_SCALE_CALCULATION_DURATION = 2 * 1000;

    private long   stableScaleCalculationStart;
    private String stableScale;  // Avoid comparing floats
    private long   restoringScaleStart;

    NoZoomedWebViewClient() {
        stableScaleCalculationStart = System.currentTimeMillis();
    }

    @Override
    public void onScaleChanged(final WebView view, float oldScale, float newScale) {
        Log.d(LOG_TAG, "onScaleChanged: " + oldScale + " -> " + newScale);

        long now = System.currentTimeMillis();
        boolean calculating = (now - stableScaleCalculationStart) < STABLE_SCALE_CALCULATION_DURATION;
        if (calculating) {
            stableScale = "" + newScale;
        } else if (!stableScale.equals("" + newScale)) {
            boolean zooming = (now - restoringScaleStart) < STABLE_SCALE_CALCULATION_DURATION;
            if (!zooming) {
                Log.d(LOG_TAG, "Zoom out to stableScale: " + stableScale);
                restoringScaleStart = now;
                view.zoomOut();

                // Just to make sure, do it one more time
                view.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        view.zoomOut();
                    }
                }, STABLE_SCALE_CALCULATION_DURATION);
            }
        }
    }
}
于 2013-06-21T03:23:36.123 回答