Android WebView use setWideViewPort, disable double-tap zoom but keep pinch zoom?

前端 未结 4 1609
無奈伤痛
無奈伤痛 2021-02-03 14:51

I am using this code and it works exactly as I want. But I have to implemented another feature on double tap and would like to disable the double tap zooming (but keeping the pi

4条回答
  •  囚心锁ツ
    2021-02-03 14:59

    The GestureDetector with "one(first) finger" solution does not work reliably. The web view is still sometimes zoomed on Galaxy S3 Android 4.0.4. An additional WebViewClient like this can be used to restore the zoom scale when the view is zoomed:

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

提交回复
热议问题