1

我已经下载了一个网页 html 和里面的图片。现在,我正在尝试向用户展示它们。我尝试了两种不同的方法,但不确定哪种方法最好。两者都有自己的问题。

首先,我尝试了一个带有以下代码的文本视图:

TextView content = (TextView)findViewById(R.id.article_content);

MyImageGetter mig = new MyImageGetter(this, urlId);
Spanned span = Html.fromHtml(contents, mig, null);
content.setText(span);

我真的很喜欢它的工作原理,除了两个问题。第一个也是最困难的是当一篇文章有​​很多图像时,我会得到 OutOfMemory fc's。MyImageGetter 代码如下

public class MyImageGetter implements Html.ImageGetter{

String urlId = null;
Context c = null;

public MyImageGetter(ArticleViewer articleViewer, String urlId2) {
    c = articleViewer;
    urlId = urlId2;
}

public Drawable getDrawable(String source) {
    String[] brokenUrl = source.split("/");
    String imgName = brokenUrl[brokenUrl.length-1];

    File image = new File("/data/data/com.that1dev.RibbonReader/Offline/" + urlId + "/" + imgName);
    Log.w("MyApp", image.getAbsolutePath());
    Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());

    Drawable d = new BitmapDrawable(c.getResources(), bm);

    d.setBounds(0,0, d.getIntrinsicWidth(),d.getIntrinsicHeight());

    return d;
}

}

另一个问题是 textview 根据用户选择和设备方向具有不同的宽度。如果视图中的图像大于 textview 的宽度,它们就会被裁剪。但是,我相信我可以自己解决这个问题而没有太大的困难。自从我尝试先解决内存问题以来,我还没有太努力。但是,非常感谢您提供任何帮助。

我尝试过的另一种方法是 webview。

WebView webContent = (WebView)findViewById(R.id.web_content);

webContent.loadDataWithBaseURL("", contents[1], "text/html", "utf-8", "");
webContent.setBackgroundColor(Color.TRANSPARENT);

不幸的是,无论我尝试什么,背景都是白色的,而且非常难看。我似乎找不到适用于 3.0 和 4.0 的解决方法。

如果我有选择,我真的很喜欢 TextView 方法,因为我更喜欢它的外观而不是 WebView 呈现事物的方式。

4

1 回答 1

1

从根本上说,您在这里尝试做的是改变 Web 内容的呈现方式 - 更换网站作者(可能是您,我不知道)所写的背景。无论如何,HTML 并不真正支持 Web 内容的透明背景,所以我唯一能想到的你可能会尝试的是通过 JavaScript 实际编辑 Web 内容:

myWebView.loadUrl("javascript:document.body.style.backgroundImage=\'\');
   document.body.style.backgroundColor=\"#00FF00\");");

(将上面的颜色替换为您选择的颜色)在 WebView 加载后调用它会清除 HTML 上的任何背景,但是当涉及到任何不在正文上的嵌套样式时,您仍然会遇到问题。

As for your image problem, you're opening all of the images at their default size and keeping them in memory. One of the things that the WebView does for you is to keep decimated (as in shrunk) renderings of the webpage images. If you want to fix your memory footprint, your best bet is to temporarily save the images to disk, and only open them when the user has scrolled to where the image needs to be - which is not going to be easy, by any means, but that's the only way to ensure that you aren't going to overflow your allocated heap space.

于 2012-04-07T19:57:04.160 回答