Android:Universal Image Loader 和空白 space 问题
Android: Universal Image Loader and the blank space issue
我有一个包含 HTML
内容(文本和图像)的字符串,我正在使用 UIL 翻译要显示的 <img>
html 标签
正确地在 TextView 中,每个图像都在其在段落中的原始位置。
一切正常,除了我在渲染图像后在 TextView 的末尾得到一个额外的空白 space。
图片越多越多space。
我在图片中显示的下方有很多空白 space。
这里有一个 link 可以查看 HTML 字符串,如果有帮助的话,我使用的字符串是 "description".
这里是 string
:
desc = (TextView) getActivity().findViewById(R.id.single_article_desc);
String ar_desc = json.getString("description");
Spanned spanned = Html.fromHtml(ar_desc,new UILImageGetter(desc, getActivity()), null);
desc.setText(spanned);
这是我正在使用的 UILImageGetter.java
:
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
public class UILImageGetter implements Html.ImageGetter {
Context c;
TextView container;
UrlImageDownloader urlDrawable;
public UILImageGetter(View textView, Context context) {
this.c = context;
this.container = (TextView) textView;
}
@Override
public Drawable getDrawable(String source) {
urlDrawable = new UrlImageDownloader(c.getResources(), source);
urlDrawable.drawable = c.getResources().getDrawable(R.drawable.no_image);
ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
return urlDrawable;
}
private class SimpleListener extends SimpleImageLoadingListener {
UrlImageDownloader urlImageDownloader;
public SimpleListener(UrlImageDownloader downloader) {
super();
urlImageDownloader = downloader;
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
int width = loadedImage.getWidth();
int height = loadedImage.getHeight();
int newWidth = width;
int newHeight = height;
if (width > container.getWidth()) {
newWidth = container.getWidth();
newHeight = (newWidth * height) / width;
}
if (view != null) {
view.getLayoutParams().width = newWidth;
view.getLayoutParams().height = newHeight;
}
Drawable result = new BitmapDrawable(c.getResources(), loadedImage);
result.setBounds(0, 0, newWidth, newHeight);
urlImageDownloader.setBounds(1, 1, newWidth, newHeight);
urlImageDownloader.drawable = result;
Log.e("new height", String.valueOf(container.getHeight() + result.getIntrinsicHeight()));// Log the new height to Logcat
container.setHeight((container.getHeight() + result.getIntrinsicHeight()));
container.invalidate();
}
}
private class UrlImageDownloader extends BitmapDrawable {
public Drawable drawable;
public UrlImageDownloader(Resources res, String filepath) {
super(res, filepath);
drawable = new BitmapDrawable(res, filepath);
}
@Override
public void draw(Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
}
}
这里是来自 Logcat 的新高度的错误日志:
02-25 17:11:28.203 2563-2563/com.example.navigationdrawer E/new height﹕ 2363
02-25 17:11:28.631 2563-2563/com.example.navigationdrawer E/new height﹕ 3038
02-25 17:11:28.719 2563-2563/com.example.navigationdrawer E/new height﹕ 3713
02-25 17:11:32.620 2563-2563/com.example.navigationdrawer E/new height﹕ 4388
02-25 17:11:32.820 2563-2563/com.example.navigationdrawer E/new height﹕ 5063
02-25 17:11:34.024 2563-2563/com.example.navigationdrawer E/new height﹕ 5738
有人可以帮忙吗?
我通过替换
完成了它
container.setHeight((container.getHeight() + result.getIntrinsicHeight()));
与
container.setHeight((container.getHeight() + newHeight));
感谢@Evripidis Drakos
我有一个包含 HTML
内容(文本和图像)的字符串,我正在使用 UIL 翻译要显示的 <img>
html 标签
正确地在 TextView 中,每个图像都在其在段落中的原始位置。
一切正常,除了我在渲染图像后在 TextView 的末尾得到一个额外的空白 space。
图片越多越多space。
我在图片中显示的下方有很多空白 space。
这里有一个 link 可以查看 HTML 字符串,如果有帮助的话,我使用的字符串是 "description".
这里是 string
:
desc = (TextView) getActivity().findViewById(R.id.single_article_desc);
String ar_desc = json.getString("description");
Spanned spanned = Html.fromHtml(ar_desc,new UILImageGetter(desc, getActivity()), null);
desc.setText(spanned);
这是我正在使用的 UILImageGetter.java
:
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
public class UILImageGetter implements Html.ImageGetter {
Context c;
TextView container;
UrlImageDownloader urlDrawable;
public UILImageGetter(View textView, Context context) {
this.c = context;
this.container = (TextView) textView;
}
@Override
public Drawable getDrawable(String source) {
urlDrawable = new UrlImageDownloader(c.getResources(), source);
urlDrawable.drawable = c.getResources().getDrawable(R.drawable.no_image);
ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
return urlDrawable;
}
private class SimpleListener extends SimpleImageLoadingListener {
UrlImageDownloader urlImageDownloader;
public SimpleListener(UrlImageDownloader downloader) {
super();
urlImageDownloader = downloader;
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
int width = loadedImage.getWidth();
int height = loadedImage.getHeight();
int newWidth = width;
int newHeight = height;
if (width > container.getWidth()) {
newWidth = container.getWidth();
newHeight = (newWidth * height) / width;
}
if (view != null) {
view.getLayoutParams().width = newWidth;
view.getLayoutParams().height = newHeight;
}
Drawable result = new BitmapDrawable(c.getResources(), loadedImage);
result.setBounds(0, 0, newWidth, newHeight);
urlImageDownloader.setBounds(1, 1, newWidth, newHeight);
urlImageDownloader.drawable = result;
Log.e("new height", String.valueOf(container.getHeight() + result.getIntrinsicHeight()));// Log the new height to Logcat
container.setHeight((container.getHeight() + result.getIntrinsicHeight()));
container.invalidate();
}
}
private class UrlImageDownloader extends BitmapDrawable {
public Drawable drawable;
public UrlImageDownloader(Resources res, String filepath) {
super(res, filepath);
drawable = new BitmapDrawable(res, filepath);
}
@Override
public void draw(Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
}
}
这里是来自 Logcat 的新高度的错误日志:
02-25 17:11:28.203 2563-2563/com.example.navigationdrawer E/new height﹕ 2363
02-25 17:11:28.631 2563-2563/com.example.navigationdrawer E/new height﹕ 3038
02-25 17:11:28.719 2563-2563/com.example.navigationdrawer E/new height﹕ 3713
02-25 17:11:32.620 2563-2563/com.example.navigationdrawer E/new height﹕ 4388
02-25 17:11:32.820 2563-2563/com.example.navigationdrawer E/new height﹕ 5063
02-25 17:11:34.024 2563-2563/com.example.navigationdrawer E/new height﹕ 5738
有人可以帮忙吗?
我通过替换
完成了它container.setHeight((container.getHeight() + result.getIntrinsicHeight()));
与
container.setHeight((container.getHeight() + newHeight));
感谢@Evripidis Drakos