美文网首页
部分手机Picasso图片加载不出来问题

部分手机Picasso图片加载不出来问题

作者: seyajin | 来源:发表于2020-11-02 09:55 被阅读0次

    使用如下代码加载图片:

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

        }

    }

    相关文章

      网友评论

          本文标题:部分手机Picasso图片加载不出来问题

          本文链接:https://www.haomeiwen.com/subject/tmuehttx.html