WebView 摆脱了双击缩放. [英] WebView getting rid of double tap zoom.

查看:16
本文介绍了WebView 摆脱了双击缩放.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多关于在 WebViews 中缩放主题的票,但没有为我的案例找到答案.

I read many tickets on the topic of Zooming in WebViews and didnt came to an answer for my case.

这是我的设置:

我正在使用自定义 webview,通常具有以下设置:

I´m using a custom webview with generally these settings:

getSettings().setBuiltInZoomControls(false);
getSettings().setSupportZoom(false);
getSettings().setUseWideViewPort(true);
getSettings().setLoadWithOverviewMode(true);

让我在此注意,我依赖于OverviewMode 以及WideViewPort 来缩放我的WebView.

Let me note right here that i depend on OverviewMode and as well on WideViewPort to Scale my WebView.

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

I´m also Overriding my OnTouchEvent and and delegate all suitable events to an Gesture detector:

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) return true;
    return super.onTouchEvent(event);
  }

这是它的侦听器实现,它拦截所有的 doubleTap 事件:

Here is its listeners implementation which is intercepting all doubleTap events:

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

我也覆盖了这 2 个与缩放相关的 WebView 方法:

Also i overrode these 2 WebView methods related to zoom:

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

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

除了所有这些选项之外,某个特定的点击频率会导致我的 web 视图放大/缩小.我还没有找到禁用这种缩放的选项,此缩放的 MotionEvent 似乎不适用于 GestureDetector 并且覆盖 zoomIn() zoomOut() 方法也没有效果.

Notherless of all these options a certain tap frequence will cause my webview to zoom in/out. I havent found an option that disables this kind of zooming, the MotionEvent for this Zoom doesnt seem to be applicable for the GestureDetector and the override zoomIn() zoomOut() methods have no effect either.

谁能帮助我避免 WebView 的这种双击缩放行为?

Can anyone help me out with a way to avoid this double tap zoom behaivior of WebView?

推荐答案

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

There are two methods to achieve your goal:

方法一

像这样实现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;
}

并将其附加到您的 GestureDetector,如下所示:

and attach it to your GestureDetector like this:

gestureDetector.setOnDoubleTapListener(this);

方法二

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

You can also use the WebSettings.setUseWideViewPort(false); and calculate the size of your view manually.

这些方法应该可以帮助您实现显示所有内容的不可缩放的 webview.

These methods should help you to achieve non-zoomable webviews that display everything.

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

这篇关于WebView 摆脱了双击缩放.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆