【问题标题】:Picasso and image size毕加索和图像尺寸
【发布时间】:2015-11-15 22:14:51
【问题描述】:

我有一个 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
          }
        ],

我可以使用其中任何一个,但到目前为止,我已经使用具有更高宽度和高度的那个解决了这个问题,并让 Picasso 执行调整大小的魔法。

那么,毕加索调整大小的成本是否足以关心它,还是我应该检查当前设备的分辨率/屏幕并下载适当的图像?如果是这种情况,我怎么知道我必须下载哪个图像?

网络消耗有明显差异,但我对调整大小部分特别好奇。

【问题讨论】:

    标签: android picasso


    【解决方案1】:
    /**
     * 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);
                }
    
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      相关资源
      最近更新 更多