【问题标题】:Android: Universal Image Loader and the blank space issueAndroid:通用图像加载器和空白空间问题
【发布时间】:2015-02-25 14:53:04
【问题描述】:

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

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

图片越多,额外的空间就越大。

我在图片中显示的内容下方有很多空白区域。

这是一个link,如果有帮助,可以查看 HTML 字符串,我使用的字符串是“描述”。

这是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()));得到?我会开始看那里
  • 我编辑了我的问题,你能再检查一下吗? @EvripidisDrakos
  • 首先,不要使用getIntrinsicHeight(),这将返回实际高度,而不是您使用的新高度。只需使用 getHeight()
  • 嗯,我解决了这个问题,我会给出答案
  • 你能支持我的回答吗:)我整天都在寻找解决这个问题。

标签: java android html universal-image-loader


【解决方案1】:

我通过替换来完成它

container.setHeight((container.getHeight() + result.getIntrinsicHeight()));

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

感谢@Evripidis Drakos

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多