将在线图片解析成ImageView

Parsing online images into ImageView

我正在尝试使用 JSON 将在线图像解析为 ImageView,为此我正在使用 Picasso 库

但由于图像尺寸较大,宽度:4608 像素和高度:2592 像素,我无法将在线图像导入 ImageView

    Picasso.with(context)
    .load(imageURL)
    .noFade()
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.ic_launcher)
    .into(viewHolder.imageView);

注意:-我成功将小尺寸图像放入 ImageView

使用这个

Picasso.with(context)
    .load(imageURL)
    .noFade()
    .fit()
    .centerCrop()
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.ic_launcher)
    .into(viewHolder.imageView);

您可以应用自定义转换。

我使用下面的方法缩放图像以保持纵横比

Transformation transformation = new Transformation() {
@Override 
public Bitmap transform(Bitmap source) {

            int targetWidth = width;
            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth  * aspectRatio);

            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);

            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }

            return result;

        }

然后

Picasso.with(context).
          load("your url").transform(transformation)
          .into(holder.iv)

查看图像转换@

http://square.github.io/picasso/

用于根据您的要求添加自定义转换

你也可以看看@

https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit/