WebView摆脱双击缩放.

Ost*_*tan 9 android zoom webview touch-event motionevent

我读了很多关于缩放WebViews主题的门票,并没有找到我的案例的答案.

这是我的设置:

我通常使用这些设置的自定义webview:

getSettings().setBuiltInZoomControls(false);
getSettings().setSupportZoom(false);
getSettings().setUseWideViewPort(true);
getSettings().setLoadWithOverviewMode(true);
Run Code Online (Sandbox Code Playgroud)

让我在这里注意,我依赖于OverviewMode以及WideViewPort来扩展我的WebView.

我还覆盖我的OnTouchEvent,并将所有合适的事件委托给Gesture检测器:

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) return true;
    return super.onTouchEvent(event);
  }
Run Code Online (Sandbox Code Playgroud)

以下是其侦听器实现,它拦截所有doubleTap事件:

  @Override
  public boolean onDoubleTapEvent(MotionEvent e) {
    // Do nothing! 
    return true;
  }

  @Override
  public boolean onDoubleTap(MotionEvent e) {
    // Do nothing! 
    return true;
  }

  @Override
  public boolean onSingleTapConfirmed(MotionEvent e) {
    // Do nothing! 
    return true;
  }
Run Code Online (Sandbox Code Playgroud)

另外我覆盖了与zoom有关的这两个WebView方法:

  @Override
  public boolean zoomIn() {
    return true;
  }

  @Override
  public boolean zoomOut() {
    return true;
  }
Run Code Online (Sandbox Code Playgroud)

除了所有这些选项之外,某个抽头频率将导致我的webview放大/缩小.我没有找到一个禁用这种缩放的选项,此Zoom的MotionEvent似乎不适用于GestureDetector,并且覆盖zoomIn()zoomOut()方法也没有效果.

任何人都可以帮助我避免这种双击缩放WebView的行为吗?

key*_*fer 6

有两种方法可以实现您的目标:

方法1

实现GestureDetector.OnDoubleTapListener如下:

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
  return false; //Nothing
}

@Override
public boolean onDoubleTap(MotionEvent e) {
  //Indicates that this implementation has handled the double tap.
  return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
  //Indicates that this implementation has handled the double tap.
  return true;
}
Run Code Online (Sandbox Code Playgroud)

并将它附加到你的GestureDetector喜欢这个:

gestureDetector.setOnDoubleTapListener(this);
Run Code Online (Sandbox Code Playgroud)

方法2

您也可以WebSettings.setUseWideViewPort(false);手动使用和计算视图的大小.

这些方法可以帮助您实现显示所有内容的非可缩放Web视图.

public int getWindowWidth(Activity activity) {
  Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  Point size = new Point();
  display.getSize(size);
  int width = size.x;
  return width;
}

public int getInitialScale(Activity activity, int websiteWidth) {
  return (getWindowWidth(activity) / websiteWidth) * 100;
}
Run Code Online (Sandbox Code Playgroud)