2015-06-30 111 views
1

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

我可以使用到这些,但到目前为止,我已经解决了使用一个具有较高的问题宽度和高度,让毕加索做调整魔术。

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

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

回答

0
/** 
* 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); 
      } 

     } 
    }); 
}