8

WebView.getScale() is deprecated (but still usable)

The recommended way to get scale for webview is to use WebViewClient.onScaleChanged() http://developer.android.com/reference/android/webkit/WebViewClient.html#onScaleChanged(android.webkit.WebView, float, float)

I've added corresponding handler in my custom webview:

public class CustomWebView extends WebView {

public CustomWebView(Context context) {
    super(context);
    setWebViewClient(new WebViewClient() {
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
            currentScale = newScale
        }
    });
}

And now the problem: the method not invoked (at least on my nexus 7) if I'm zoom in/zoom out using pinch, so it works good only if I use zoom controls button.

4

2 回答 2

3

我正在为 API 级别 17 进行开发,出于同样的原因,我最终使用了已弃用的方法 getScale()。当我在 WebView 中使用捏缩放更改比例时,根本没有触发 WebViewClient.onScaleChanged()。我的扩展 WebView 中有一个自定义 GestureListener,当视图放大(缩放 1.0+)时,它的行为会有所不同。当我在 onScaleChanged() 中设置类变量时,我无法可靠地获得正确的比例值。

于 2013-07-26T15:24:58.097 回答
1

尝试 :

public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    setWebViewClient(new WebViewClient() {
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
            currentScale = newScale;
        }
    });
}
于 2013-07-11T12:21:19.900 回答