看不见的网页流量已经固定尺寸(大于父) [英] Invisible webview that has fixed size (bigger than parent)

查看:118
本文介绍了看不见的网页流量已经固定尺寸(大于父)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个网页视图,我需要设置一个固定的大小。

I need to create a webview that I need to set a fixed size.

说出的WebView应该有一个570像素宽度,
但该设备只具有320像素的宽度。

Say the webview should have a pixel width of 570, but the device only has a 320px width.

我如何使网页流量大小是无关紧要的父母的大小?

How do I render the webview size that is irrelevant to the parents size?

final WebView webView = new WebView(context);
webView.loadDataWithBaseURL("", html, "text/html", "utf-8", null);

webView.setDrawingCacheEnabled(true);
webView.setVisibility(View.INVISIBLE);
rootView.addView(webView);

webView.setWebViewClient(new CustomtWebClient(context));

在CustomtWebClient

In the CustomtWebClient

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    final WebView webView = view;
    view.postDelayed(new Runnable() {
        @Override
        public void run() {
            Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(),
                    webView.getContentHeight() * (int) webView.getScale(), Bitmap.Config.ARGB_8888);
            final Canvas c = new Canvas(bitmap);
            webView.draw(c);

            //additional stuff...

        }
    }, 100);
}

正如你可以看到的WebView应该只被渲染,然后变成了一个位图。

As you can see the webview should only be rendered and then turned into a bitmap.

但我的方式去做,现在web视图仅限于父母的大小,它是基于设备上。

But the way I do it now the webview is restricted to the parents size, which is based on the device.

我需要一个独立的WebView。

I need an independent webview.

推荐答案

您应该能有一个关闭屏幕的WebView 没有它附加到视图层次结构(这是无需调用 rootView.addView

You should be able to have an 'off screen' WebView without attaching it to the view hierarchy (that is without calling rootView.addView):

final WebView webView = new WebView(context);
webView.loadDataWithBaseURL("", html, "text/html", "utf-8", null);

webView.layout(0, 0, desiredWidth, desiredHeight);
webView.setWebViewClient(new CustomtWebClient(context));

不幸的是你接近使用 onPageFinished 将无法可靠地工作 - 这是太早了,你有时会(或在某些设备上)得到一个白色的页面。目前的WebView不具有知道什么时候它有好东西画的良好回调。

Unfortunately you approach of using onPageFinished will not work reliably - it's too early and you'll sometimes (or on some devices) get a white page. Currently the WebView doesn't have a good callback for knowing when it has "something good to draw".

你能做的最好的可能是pcated去$ P $ setPictureListener 和任意超时的组合(比如100毫秒)。另外,您可以呈现一个小位图第一和检查它是否是全白。

The best you could do is probably a combination of the deprecated setPictureListener and an arbitrary timeout (say 100ms). Alternatively you could render to a small bitmap first and check if it's completely white.

这篇关于看不见的网页流量已经固定尺寸(大于父)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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