2014-10-05 65 views
3

我的活动中有一个固定标题,我需要将图像动态加载到其中。我需要这个图像来完全填充这个标题,保持宽度和高度的比例。设置使用毕加索加载图像的最大宽度

如果我以前加载图像,我得到OutOfMemoryException。如果我使用毕加索的fit()方法,则该区域会有空白区域。如果我使用Transformation作为StackOverflow中的一些人所建议的,我也会得到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); 

UPDATE

我想通了毕加索错估的大小当我加载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; 
    } 

回答

3

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

+0

我试过了centerCrop()和centerInside()。 – 2014-10-06 14:20:16

+0

好的,但这就是你如何去做,除非我完全误解你的问题。也许你有另一个问题? – 2014-10-06 14:24:26

+0

***重要*** @Jacob在GitHub上发布了解决此问题的解决方法:https://github.com/square/picasso/issues/691 – 2014-10-11 18:31:18

2

的ImageView的必须加载图像之前被添加到ViewFlipper。这是为了让毕加索知道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); 
+0

同样的问题仍然存在。 – 2014-10-05 23:41:29

+0

请发布您的布局xml文件。我怀疑空格可能在那里。 – 2014-10-05 23:42:37

+0

您需要在加载图像之前将ImageView添加到布局。在图像加载的位置,它没有测量ImageView的宽度/高度,因此无法将图像缩放到相同的大小。 – 2014-10-05 23:55:58