通用图像加载器:获取原始高度和宽度on

时间:2016-04-24 18:56:02

标签: android html universal-image-loader

我使用Universal Image Loader在Jsoup解析的html中加载图像。 <img>标记没有静态位置,它们可以出现在Html元素中的任何位置。由于我希望它们出现在<img>所在的位置,因此我无法为它们提供图像视图。

这是我用来加载图片的类

public class UILImageGetter implements Html.ImageGetter, View.OnClickListener{
    Context c;
    TextView conatiner;
    UrlImageDownloader urlDrawable;

    public UILImageGetter(View textView, Context context) {
        this.c = context;
        this.conatiner = (TextView) textView;
    }

    @Override
    public Drawable getDrawable(String source) {
        urlDrawable = new UrlImageDownloader(c.getResources(), source);
        if (Build.VERSION.SDK_INT >= 21) {
        urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb,null);
        } else {
            urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb);
        }
        ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
        return urlDrawable;
    }

    @Override
    public void onClick(View v) {

    }

    private class SimpleListener extends SimpleImageLoadingListener {
        UrlImageDownloader mUrlImageDownloader;

        public SimpleListener(UrlImageDownloader downloader) {
            super();
            mUrlImageDownloader= 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 > conatiner.getWidth()) {
                newWidth = conatiner.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);

            mUrlImageDownloader.setBounds(0, 0, newWidth, newHeight);
            mUrlImageDownloader.mDrawable = result;

            conatiner.setHeight((conatiner.getHeight() + result.getIntrinsicHeight()));
            //conatiner.invalidate();
        }
    }

    private class UrlImageDownloader extends BitmapDrawable {
        public  Drawable mDrawable;

        public UrlImageDownloader(Resources resources, String filepath) {
            super(resources, filepath);
            mDrawable = new BitmapDrawable(resources, filepath);
        }

        @Override
        public void draw(Canvas canvas) {
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
        }
    }
}

我的问题是如何在图像上设置OnclickListener并让它们在单击时显示(在对话框中)原始高度和宽度。

0 个答案:

没有答案