部分手機(jī)Picasso圖片加載不出來問題

使用如下代碼加載圖片:

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";

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容