【问题标题】:Picasso Android Only Load Images above a Certain ScalePicasso Android 仅加载特定比例以上的图像
【发布时间】:2015-02-17 13:55:14
【问题描述】:

我正在使用毕加索图书馆将图像加载到列表视图中,形成几个不同的在线资源。然而,有些图像太小,最终在将它们应用到视图时看起来很模糊。有没有办法在使用毕加索加载图像时检查图像的大小?这是我的代码:

 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 方法的源参数的比例,但宽度始终为:666。有关如何执行此操作的任何建议?

谢谢

【问题讨论】:

  • 您正在使用调整大小以及 centerInside。尝试只使用其中一个

标签: android picasso


【解决方案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);
}

希望这会有所帮助

【讨论】:

  • 我做到了。但既然我这样做了: .resize(size, size) 位图将在到达 onBitmapLoaded 方法的时间之前调整大小。
猜你喜欢
  • 2016-07-06
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
  • 2018-06-27
  • 2016-02-08
  • 2018-07-14
相关资源
最近更新 更多