Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

android - WebView getting rid of double tap zoom.

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

Here′s my setup:

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

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

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

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

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

Also i overrode these 2 WebView methods related to zoom:

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

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two methods to achieve your goal:

Method 1

Implement the GestureDetector.OnDoubleTapListener like this:

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

and attach it to your GestureDetector like this:

gestureDetector.setOnDoubleTapListener(this);

Method 2

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...