通用图像装载机获得原始图像大小 [英] Universal Image Loader get original image size

查看:147
本文介绍了通用图像装载机获得原始图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是Android系统的通用 - 图像 - 装载机库加载/缓存远程图像,并已通过源挖了好一阵子试图找到一种方法来检索原始图像大小(宽度和高度)为我ImageLoadingListener。

I am using the Android-Universal-Image-Loader library to loading/caching remote images, and have been digging through the source for quite a while trying to find a way to retrieve the original image size (width and height) for my ImageLoadingListener.

下面的示例code只是给你什么,我试图做一个想法。

The sample code below is just give you an idea of what I'm trying to do.

    protected class ViaImageLoadingListener implements ImageLoadingListener {
    final SelectableImageView selectableImageView ;


    protected ViaImageLoadingListener(SelectableImageView selectableImageView) {
        this.selectableImageView = selectableImageView;
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            selectableImageView.setImageBitmap(loadedImage);
            // loadedImage.getWeight() will not return the original 
            // dimensions of the image if it has been scaled down
            selectableImageView.setOriginalImageSize(width, height);
            selectableImageView.invalidate();


    }

我曾尝试延长ImageDe codeR类和ImageLoader的类来找到一个迂回的方式来链接去codeR(从中我可以在#prepareDecodingOptions 方法),以我的自定义ImageLoadingListener。但配置对象是私营和领域(包括德codeR)无法访问从子类(和感觉反正解决问题的过度哈克的方式)。

I have tried extending the ImageDecoder class and the ImageLoader class to find a round-about way to linking the decoder (from which I can get the original image size in the #prepareDecodingOptions method) to my custom ImageLoadingListener. But the configuration object is private and the fields (including the decoder) are inaccessible from subclasses (and feels like an overly hacky way of solving the problem anyways).

我是否忽略了获得原始图像大小不失UIL的缩放/内存管理的好处的一个简单的内置的方式?

Have I overlooked a simple "built-in" way of getting the original image size without losing the benefit of the UIL's scaling/memory management?

推荐答案

有没有办法通过从ImageDe codeR原始图像的大小,通过PARAMS到监听器。

There is no way to pass original image size from ImageDecoder to listener through params.

我认为您的解决方案是继。
延长 BaseImageDe codeR 并在其中创建映射保持图像大小:

I think the solution for you is following. Extend BaseImageDecoder and create map in it for keeping image sizes:

地图<弦乐,IMAGESIZE> urlToSizeMap =新的ConcurrentHashMap<弦乐,IMAGESIZE>();

然后重写 defineImageSizeAndRotation(...)

protected ImageFileInfo defineImageSizeAndRotation(InputStream imageStream, String imageUri) throws IOException {
    ImageFileInfo info = super.defineImageSizeAndRotation(imageStream, imageUri);
    urlToSizeMap.put(imageUri, info.imageSize); // Remember original image size for image URI
    return info;
}

请注意: info.imageSize 不能编译,因为 IMAGESIZE 是不可见的。我将在下一版本修复它(1.8.5),但你可以使用反射现在。

Note: info.imageSize won't compile because imageSize isn't visible. I'll fix it in next version (1.8.5) but you can use reflection for now.

设置此德codeR到配置,并保持参照本德codeR任何地方(或者你可以让 urlToSizeMap 静态从听众访问)。

Set this decoder into configuration and keep reference to this decoder anywhere (or you can make urlToSizeMap static to access from listener).

然后在您的监听器:

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        selectableImageView.setImageBitmap(loadedImage);

        ImageSize imageSize = decoder.urlToSizeMap.get(imageUri);
        selectableImageView.setOriginalImageSize(imageSize.getWidth(), imageSize.getHeight());
        selectableImageView.invalidate();
}

这篇关于通用图像装载机获得原始图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆