Android:Universal Image Loader和空白问题

时间:2015-02-25 14:53:04

标签: java android html universal-image-loader

我有一个包含HTML内容(文字和图片)的字符串,我使用UIL翻译要显示的<img>个html标签 正确地在TextView中,每个图像在段落中的原始位置。

一切正常,只是在渲染图像后我在TextView的末尾得到一个空白的额外空格。

图像越多,额外的空间越多。

This is the demo

在我在图片中显示的内容下,还有很多空白。

这里有一个link来查看HTML字符串,如果有帮助的话,我使用的字符串是&#34; description&#34;。

这里是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 

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我通过替换

完成了
container.setHeight((container.getHeight() + result.getIntrinsicHeight()));

container.setHeight((container.getHeight() + newHeight));

谢谢@Evripidis Drakos