Picasso Android仅加载一定规模以上的图像

时间:2015-02-17 05:02:26

标签: android picasso

我正在使用Picasso Library将图像加载到Listview表单中,形成几个不同的在线资源。但是,某些图像太小,在将它们应用到视图时最终看起来模糊不清。有没有办法检查使用Picasso加载图像时图像的大小?这是我的代码:

 Picasso.with(MainActivity.context)
                    .load(imgUrl)
                    .transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT))
                    .skipMemoryCache()
                    .resize(size, size)
                    .centerInside()
                    .into(target);

变换:

class BitmapTransform implements Transformation {

int maxWidth;
int maxHeight;

public BitmapTransform(int maxWidth, int maxHeight) {
    this.maxWidth = maxWidth;
    this.maxHeight = maxHeight;
}

@Override
public Bitmap transform(Bitmap source) {
    int targetWidth, targetHeight;
    double aspectRatio;
    int maxWidthTmp = maxWidth;
    int maxHeightTmp = maxHeight;

    if (source.getWidth() > source.getHeight()) {
        targetWidth = maxWidthTmp;
        aspectRatio = (double) source.getHeight() / (double) source.getWidth();
        targetHeight = (int) (targetWidth * aspectRatio);
    } else {
        targetHeight = maxHeightTmp;
        aspectRatio = (double) source.getWidth() / (double) source.getHeight();
        targetWidth = (int) (targetHeight * aspectRatio);
    }

    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
    if (result != source) {
        source.recycle();
    }
    return result;
}

@Override
public String key() {
    return maxWidth + "x" + maxHeight;
}

};

我试图检查进入Transform方法的Source Paramater的比例,但宽度始终为:666。有关如何执行此操作的任何建议吗?

谢谢

1 个答案:

答案 0 :(得分:1)

试试这个

private Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)      {  
                int width =bitmap.getWidth();
                int height =bitmap.getHeight();
            }
            @Override
            public void onBitmapFailed() {
            }
      }

private void someMethod() {
   Picasso.with(this).load("url").into(target);
}

希望这会有所帮助