使用如下代碼加載圖片:
Picasso.with(getContext()).load(Uri.parse(imageFullPath))
? ? ? ? ? ? ? ? ? ? .resize(newWidth,newHeight)
? ? ? ? ? ? ? ? ? ? .transform(new RoundedCornersTransform(30))
? ? ? ? ? ? ? ? ? ? .into(image);
在部分手機(jī)上會出現(xiàn)圖片無法正常加載的情況,原因是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";
? ? }
}