gpt4 book ai didi

java - textView 中的 Android HTML,图像大小

转载 作者:行者123 更新时间:2023-11-30 01:54:59 24 4
gpt4 key购买 nike

我需要用 HTML 实现 textview,通常是图像。我使用了在这里找到的代码: Android HTML.fromHTML() with images?

URLDrawable.java

public class URLDrawable extends BitmapDrawable {
// the drawable that you need to set, you could set the initial drawing
// with the loading image if you need to
protected Drawable drawable;

@Override
public void draw(Canvas canvas) {
// override the draw to facilitate refresh function later
if(drawable != null) {
drawable.draw(canvas);
}
}
}

URLImageParser.java

public class URLImageParser implements ImageGetter {
Context c;
View container;

/***
* Construct the URLImageParser which will execute AsyncTask and refresh the container
* @param t
* @param c
*/
public URLImageParser(View t, Context c) {
this.c = c;
this.container = t;
}

public Drawable getDrawable(String source) {
URLDrawable urlDrawable = new URLDrawable();

// get the actual source
ImageGetterAsyncTask asyncTask =
new ImageGetterAsyncTask( urlDrawable);

asyncTask.execute(source);

// return reference to URLDrawable where I will change with actual image from
// the src tag
return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;

public ImageGetterAsyncTask(URLDrawable d) {
this.urlDrawable = d;
}

@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}

@Override
protected void onPostExecute(Drawable result) {
// set the correct bound according to the result from HTTP call
urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0
+ result.getIntrinsicHeight());

// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;

// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
}

/***
* Get the Drawable from URL
* @param urlString
* @return
*/
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
+ drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
return null;
}
}

private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
}

我在这里设置我的 TextView 文本:

URLImageParser parser = new URLImageParser(textView, getActivity());
Spanned spanned = Html.fromHtml(myArticle.getDescription(), parser, null);
textView.setText(spanned);

但是我得到的图片太小了:

enter image description here

如果我改变 drawable.setBounds(0, 0, 500(just example), 500(just example))

我遇到了这个问题:

enter image description here

我刚刚开始使用 android,请帮助我。

最佳答案

感谢 Bonatti 的评论。这是使用 WebView 的最佳方式

关于java - textView 中的 Android HTML,图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32395460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com