【问题标题】:Android -- Displaying background WebView as a BitmapAndroid -- 将背景 WebView 显示为位图
【发布时间】:2011-02-15 05:56:08
【问题描述】:

我想在后台加载一个 webview 并且只显示它加载的页面的位图。我的视图层次结构中有一个 webview,其可见性设置为“不可见”,还有一个我希望显示 webview 位图的 imageview。

imageView = (ImageView)findViewById(R.id.imageview);
webView = (WebView)findViewById(R.id.webview);
webView.setBackgroundColor(Color.parseColor("#006330"));

webView.setDrawingCacheEnabled(true);
webView.loadDataWithBaseURL(SiteAPI.URL, html, "text/html", "iso-8859-1", null);

webView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());

Bitmap b = webView.getDrawingCache(); 
webView.setDrawingCacheEnabled(false);          

if(b != null) {
     Toast.makeText(getBaseContext(), "Not null", Toast.LENGTH_LONG).show();
     imageView.setImageBitmap(Bitmap.createBitmap(b));
}
else {
     Toast.makeText(getBaseContext(), "Null.", Toast.LENGTH_LONG).show();
}

对 getDrawingCache() 的调用每次都返回 null。我试过 setContentView(webView) 并且页面显示正常。我确保 isDrawingCacheEnabled() 返回 true,但位图仍然为空。我哪里错了?

【问题讨论】:

    标签: android webview imageview


    【解决方案1】:

    你有两个选择:

    1. 使用webView.capturePicture() 方法,但请注意,这将返回整个网页的图片,因此您必须对其进行裁剪以仅显示您想要的部分。

    2. 使用 webView.draw(Canvas) 在所需的 Canvas 对象中手动绘制 webView。

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多