我有一个ListView,在它里面,每一行都有一个文本和一张图片,下载并与毕加索一起显示。
我使用的图片来自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
}
],我可以使用它们中的任何一个,但到目前为止,我已经解决了问题,使用了具有更高宽度和高度的那个,并让毕加索来做调整大小的魔术。
那么,毕加索调整大小的成本是否足以让我关心它,或者我是否应该检查当前设备的分辨率/屏幕并下载适当的图像?如果是这样的话,我如何知道我必须下载哪个镜像?
在网络消耗上有明显的区别,但我特别好奇的是调整大小的部分。
发布于 2015-07-01 21:37:33
/**
* 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);
}
}
});
}https://stackoverflow.com/questions/31149773
复制相似问题