毕加索和图像大小

时间:2015-06-30 22:41:42

标签: android picasso

我有一个ListView,在其中,每一行都有一个文本,一个图像下载并与Picasso一起显示。

我使用的图像来自Spotify API,它提供不同的尺寸,例如:

"images": [
          {
            "height": 789,
            "url": "https://i.scdn.co/image/99afd5b3e7ce4b82fdc007dc5ed8dfe0806f6fe2",
            "width": 779
          },
          {
            "height": 648,
            "url": "https://i.scdn.co/image/68e20f364ba16a4386d8f55ca6bed5fb8da3136d",
            "width": 640
          },
          {
            "height": 203,
            "url": "https://i.scdn.co/image/8e68acfb185a7370a3c4efdbdd42b3e1a5c82ac8",
            "width": 200
          },
          {
            "height": 65,
            "url": "https://i.scdn.co/image/a86ea149077b22239f41e8b17f7261c475b084ee",
            "width": 64
          }
        ],

我可以使用其中的任何一个,但到目前为止,我已经使用宽度和高度更高的问题解决了问题,并让Picasso做了调整魔法。

那么,毕加索重新调整大小的成本是否足以关注它,还是我应该检查当前设备的分辨率/屏幕并下载合适的图像?如果是这种情况,我怎么知道我必须下载哪个图像?

网络消费存在明显差异,但我对调整大小部分特别好奇。

1 个答案:

答案 0 :(得分:1)

/**
 * this method will apply the transformation and will automatically resize the images as per screen
 * 
 * @param lessWidth
 *             the value with which the width must be decreased for the image resize with respect to screen width
 */
public void apply(final int lessWidth)
{
    // TODO Auto-generated method stub
    Transformation transformation = new Transformation()
    {

        @Override
        public Bitmap transform(Bitmap source)
        {
            sourceBitmap = source;
            int targetWidth = context.getResources().getDisplayMetrics().widthPixels - lessWidth;
            if (source.getWidth() > targetWidth)
            {
                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (targetWidth * aspectRatio);
                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                if (result != source)
                {
                    source.recycle();
                }
                return result;
            }
            return source;
        }

        @Override
        public String key()
        {
            return "transformation" + " desiredWidth";
        }
    };
    if (strImageUrl == null || strImageUrl == "")
    {
        return;
    }

    Picasso.with(context).load(strImageUrl).transform(transformation).into(imageView, new com.squareup.picasso.Callback()
    {

        @Override
        public void onSuccess()
        {
            MyLog.d("PicassoHelper", "ImageLoadSuccess: url=" + strImageUrl);

        }

        @Override
        public void onError()
        {
            MyLog.d("PicassoHelper", "ImageLoadError: url=" + strImageUrl);
            if (imageView != null && defaultBitmap != null)
            {
                imageView.setImageBitmap(defaultBitmap);
            }

        }
    });
}