设置最大宽度以使用Picasso加载图像

时间:2014-10-05 23:24:44

标签: android picasso

我的活动中有一个固定的标题,我需要动态加载图像。我需要这个图像来完全填充这个标题,保持宽度和高度的比例。

如果我之前加载图像,我会得到OutOfMemoryException。如果我使用Picasso的fit()方法,该区域将有空格。如果我像StackOverflow中的某些人建议的那样使用Transformation,我也会得到OutOfMemoryException。

我该如何解决这个问题?

标题XML:

<FrameLayout
        android:id="@+id/headerContainer"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#ffd5d5d5">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView4"
            android:layout_gravity="center"
            android:src="@drawable/background_camera" />

        <ViewFlipper
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewFlipper"
            android:layout_gravity="center" >

        </ViewFlipper>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Adicionar foto do estacionamento"
            android:id="@+id/textAddPhotos"
            android:layout_gravity="center"
            android:textColor="#ff757575"
            android:padding="10dp" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:id="@+id/addPhotosArea"></RelativeLayout>

    </FrameLayout>

使用以下代码以编程方式将图像添加到ViewFlipper中:

final ImageView imageView = new ImageView(SuggestionActivity.this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        Picasso.with(SuggestionActivity.this).load(selectedImageUri)
                .fit().centerInside()
                .into(imageView);

        mViewFlipper.addView(imageView);

更新

当我加载exif方向=90º的图像时,我发现毕加索错误地计算了尺寸。

我正在尝试更改其来源,但逻辑非常令人困惑。

我认为问题出在BitmapHunter.java文件的这个函数中:

static Bitmap transformResult(Request data, Bitmap result, int exifRotation) {
    int inWidth = result.getWidth();
    int inHeight = result.getHeight();

    int drawX = 0;
    int drawY = 0;
    int drawWidth = inWidth;
    int drawHeight = inHeight;

    Matrix matrix = new Matrix();

    if (data.needsMatrixTransform()) {
      int targetWidth = data.targetWidth;
      int targetHeight = data.targetHeight;

      float targetRotation = data.rotationDegrees;
      if (targetRotation != 0) {
        if (data.hasRotationPivot) {
          matrix.setRotate(targetRotation, data.rotationPivotX, data.rotationPivotY);
        } else {
          matrix.setRotate(targetRotation);
        }
      }

      if (data.centerCrop) {
        float widthRatio = targetWidth / (float) inWidth;
        float heightRatio = targetHeight / (float) inHeight;
        float scale;
        if (widthRatio > heightRatio) {
          scale = widthRatio;
          int newSize = (int) Math.ceil(inHeight * (heightRatio / widthRatio));
          drawY = (inHeight - newSize) / 2;
          drawHeight = newSize;
        } else {
          scale = heightRatio;
          int newSize = (int) Math.ceil(inWidth * (widthRatio / heightRatio));
          drawX = (inWidth - newSize) / 2;
          drawWidth = newSize;
        }
        matrix.preScale(scale, scale);
      } else if (data.centerInside) {
        float widthRatio = targetWidth / (float) inWidth;
        float heightRatio = targetHeight / (float) inHeight;
        float scale = widthRatio < heightRatio ? widthRatio : heightRatio;
        matrix.preScale(scale, scale);
      } else if (targetWidth != 0 && targetHeight != 0 //
          && (targetWidth != inWidth || targetHeight != inHeight)) {
        // If an explicit target size has been specified and they do not match the results bounds,
        // pre-scale the existing matrix appropriately.
        float sx = targetWidth / (float) inWidth;
        float sy = targetHeight / (float) inHeight;
        matrix.preScale(sx, sy);
      }
    }

    if (exifRotation != 0) {
      matrix.preRotate(exifRotation);
    }

    Bitmap newResult =
        Bitmap.createBitmap(result, drawX, drawY, drawWidth, drawHeight, matrix, true);
    if (newResult != result) {
      result.recycle();
      result = newResult;
    }

    return result;
  }

2 个答案:

答案 0 :(得分:3)

使用.fit()。centerCrop(),它应该做你想要的。

答案 1 :(得分:2)

在加载图像之前,必须将ImageView添加到ViewFlipper。这样Picasso就知道了ImageView的高度/宽度,它需要将图像正确缩放到ImageView的大小。

将其更改为:

final ImageView imageView = new ImageView(SuggestionActivity.this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

mViewFlipper.addView(imageView);

Picasso.with(SuggestionActivity.this).load(selectedImageUri)
        .fit().centerInside()
        .into(imageView);