使用如下代码加载图片:
Picasso.with(getContext()).load(Uri.parse(imageFullPath))
.resize(newWidth,newHeight)
.transform(new RoundedCornersTransform(30))
.into(image);
在部分手机上会出现图片无法正常加载的情况,原因是resize操作和transform操作可能存在某种冲突。
解决方案:
不使用resize方法来缩减图片,使用BitmapScaleTransform,代码如下:
public class BitmapScaleTransform implements Transformation {
private int maxWidthHeight;
public BitmapScaleTransform(int maxWidthHeight) {
this.maxWidthHeight = maxWidthHeight;
}
@Override
public Bitmap transform(Bitmap source) {
int width = source.getWidth();
int height = source.getHeight();
int targetWidth = 0;
int targetHeight = 0;
if (width != 0 && height != 0) {
float ratio = (float) width / height;
if (width > height) {
targetWidth = maxWidthHeight;
targetHeight = (int) (maxWidthHeight / ratio);
} else {
targetHeight = maxWidthHeight;
targetWidth = (int) (maxWidthHeight * ratio);
}
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
source.recycle();
}
return result;
}
return source;
}
@Override
public String key() {
return "bitmap_scale";
}
}