禁用的WebView双卡放大 [英] Disabling the WebView double-tab zoom

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

问题描述

我显示在Android的WebView网页。该页面需要进行调整以适合web视图的可用宽度。

的HTML(宽度550只是一个例子,也可以改变):

 < HTML>
< HEAD>
< META NAME =视CONTENT =WIDTH = 550>
...
 

Java的:

  mWebView =(web视图)findViewById(R.id.webView);
mWebView.getSettings()setJavaScriptEnabled(真)。
mWebView.setHorizo​​ntalScrollBarEnabled(假);
mWebView.setVerticalScrollBarEnabled(假);
mWebView.requestFocus(View.FOCUS_DOWN);
。mWebView.getSettings()setLoadWithOverviewMode(真正的); //需要缩放
。mWebView.getSettings()setUseWideViewPort(真正的); //需要缩放
mWebView.loadUrl(someUrl);
 

这工作正常,但一个问题:双卡触发概览模式和默认比例之间的切换
。 有什么办法禁用此功能?

顺便说一句:我不能计算在code的规模,因为加载页面可能有不同的尺寸,这是我事先不知道。我也已经检查的WebView源$ C ​​$ C,却看不到任何东西,我可以代替或改变。<​​/ P>

解决方案

您可以编写自己的WebView实现implemting OnGestureListener:

 公共类MyWebView扩展的WebView实现OnGestureListener
 

在声明GestureDetector:

 私人GestureDetector gestureDetector;
 

实现一个initWebView()方法,并调用它在这样的构造:

  //最好的,你做的每一个WebViewConstructor:
  公共AppWebView(上下文的背景下){
  超(上下文);
  initWebView(); }

私人无效initWebView(){
   gestureDetector =新GestureDetector(的getContext(),这一点);
   如果你喜欢//是否有任何其他自定义你的web视图。
}
 

现在实现从OnGestureListener方法并返回true上双击事件:

 公共布尔onSingleTapConfirmed(MotionEvent E){
    返回false; //没有
  }

  公共布尔onDoubleTap(MotionEvent E){
    返回true; //没有
  }

  公共布尔onDoubleTapEvent(MotionEvent E){
    返回true; //没有
  }

  公共布尔onDown(MotionEvent E){
    返回false; //没有
  }

  公共无效OnShow中preSS(MotionEvent E){
    //没有
  }

  公共布尔onSingleTapUp(MotionEvent E){
    返回false; //没有
  }

  公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,浮distanceY){
    返回false; //没有
  }

  公共无效onLong preSS(MotionEvent E){
    //没有
  }

  公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
    返回false; //没有
  }
 

最后覆盖您的WebView中的onTouchEvent,让它穿过事件手势检测是这样的:

  @覆盖
  公共布尔的onTouchEvent(MotionEvent事件){
    如果(gestureDetector.onTouchEvent(事件))回归真实;
    返回super.onTouchEvent(事件);
  }
 

本作品pretty的好一般,但有1基本缺陷这一解决方案:未认定为​​DoubleTap事件的一些活动可能会导致你的web视图的放大。

I'm仍然在为一个解决方案,将在这里发布我的进步: 的WebView摆脱双击缩放。

I am showing a webpage in an Android WebView. The page needs to be scaled to fit the available width of the WebView.

HTML (width 550 is just an example, that could change):

<html> 
<head> 
<meta name="viewport" content="width=550">
...

Java:

mWebView = (WebView)findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.requestFocus(View.FOCUS_DOWN);
mWebView.getSettings().setLoadWithOverviewMode(true); // required for scaling
mWebView.getSettings().setUseWideViewPort(true); // required for scaling
mWebView.loadUrl(someUrl);

This works fine, except one problem: double-tab triggers a switch between overview mode and default scale.
Is there any way to disable this functionality?

Btw: I cannot calculate the scale in the code, because the loaded pages might have different sizes, which I do not know beforehand. I also already checked the source code of WebView, but do not see anything, that I could override or change.

解决方案

You could write your own WebView implementation implemting OnGestureListener:

public class MyWebView extends WebView implements OnGestureListener

Inside declare a GestureDetector:

  private GestureDetector             gestureDetector;

Implement a initWebView() method and call it in the constructor like this:

// Best you do this for every WebViewConstructor:
  public AppWebView(Context context) {
  super(context);
  initWebView(); }

private void initWebView(){
   gestureDetector = new GestureDetector(getContext(), this);
   // Do any other customizations for your webview if you like. 
}

Now implement the methods from OnGestureListener and return true on double tap events:

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

  public boolean onDoubleTap(MotionEvent e) {
    return true; //Nothing
  }

  public boolean onDoubleTapEvent(MotionEvent e) {
    return true; //Nothing
  }

  public boolean onDown(MotionEvent e) {
    return false; //Nothing
  }

  public void onShowPress(MotionEvent e) {
    //Nothing
  }

  public boolean onSingleTapUp(MotionEvent e) {
    return false; //Nothing
  }

  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false; //Nothing
  }

  public void onLongPress(MotionEvent e) {
    //Nothing
  }

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    return false; //Nothing
  }

Finally Override OnTouchEvent of your webview and let it pass through events to the gesture detector like this:

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

This works pretty good generally but there is 1 basic flaw to this solution: Some Events that are not identified as DoubleTap events could cause the Zoom of your webview.

I´m still working on a solution for that and will post my progress here: WebView getting rid of double tap zoom.

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

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