在 Android WebView 中启用/禁用缩放 [英] enable/disable zoom in Android WebView

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

问题描述

WebSettings 中有一些与缩放相关的方法:

There are some methods in WebSettings related to zoom:

  • WebSettings.setSupportZoom
  • WebSettings.setBuiltInZoomControls

我注意到它们在某些设备上的工作方式有所不同.例如,在我的 Galaxy S 上默认启用捏合缩放,但在 LG P500 上它被禁用(现在我不知道如何仅启用捏合缩放,但隐藏缩放按钮).

I noticed they work differently on some devices. For example, on my Galaxy S pinch to zoom is enabled by default, but on LG P500 it is disabled (And now I don't know how to enable ONLY pinch to zoom, but hide zooming buttons).

在 P500 上,当我调用 setBuiltInZoomControls(true) 时,这两种变体都可以工作(多点触控和按钮).

On P500 when I call setBuiltInZoomControls(true) I get both these variants working (multitouch and buttons).

如何在 LG P500 等设备上启用多点触控缩放和禁用缩放按钮?(另外,我知道 HTC 设备也有同样的问题)

How to enable multitouch zoom and disable zooming buttons on devices such an LG P500? (Also, I know the same problems are on HTC devices)

更新:这里是解决方案的几乎完整代码

UPDATE: Here is almost full code for the solution

if (ev.getAction() == MotionEvent.ACTION_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_1_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_2_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
    if (multiTouchZoom && !buttonsZoom) {
        if (getPointerCount(ev) > 1) {
            getSettings().setBuiltInZoomControls(true);
            getSettings().setSupportZoom(true);
        } else {
            getSettings().setBuiltInZoomControls(false);
            getSettings().setSupportZoom(false);
        }
    }
}

if (!multiTouchZoom && buttonsZoom) {
    if (getPointerCount(ev) > 1) {
        return true;
    }
}

此代码在我的 onTouchEvent 覆盖的 WebView 方法中.

This code is in my onTouchEvent overridden method of the WebView.

推荐答案

我查看了 WebView 的源代码,我得出的结论是没有优雅的方法来完成您的要求.

I've looked at the source code for WebView and I concluded that there is no elegant way to accomplish what you are asking.

我最终做的是子类化 WebView 并覆盖 OnTouchEvent.在 ACTION_DOWNOnTouchEvent 中,我使用 MotionEvent.getPointerCount() 检查有多少指针.如果有多个指针,则调用setSupportZoom(true),否则调用setSupportZoom(false).然后我调用 super.OnTouchEvent().

What I ended up doing was subclassing WebView and overriding OnTouchEvent. In OnTouchEvent for ACTION_DOWN, I check how many pointers there are using MotionEvent.getPointerCount(). If there is more than one pointer, I call setSupportZoom(true), otherwise I call setSupportZoom(false). I then call the super.OnTouchEvent().

这将在滚动时有效地禁用缩放(从而禁用缩放控件)并在用户将要捏合缩放时启用缩放.这不是一个很好的方法,但到目前为止它对我来说效果很好.

This will effectively disable zooming when scrolling (thus disabling the zoom controls) and enable zooming when the user is about to pinch zoom. Not a nice way of doing it, but it has worked well for me so far.

请注意,getPointerCount() 是在 2.1 中引入的,因此如果您支持 1.6,则必须做一些额外的事情.

Note that getPointerCount() was introduced in 2.1 so you'll have to do some extra stuff if you support 1.6.

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

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