我的活動中有一個固定標題,我需要將圖像動態加載到其中。我需要這個圖像來完全填充這個標題,保持寬度和高度的比例。設置使用畢加索加載圖像的最大寬度
如果我以前加載圖像,我得到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;
}
我試過了centerCrop()和centerInside()。 – 2014-10-06 14:20:16
好的,但這就是你如何去做,除非我完全誤解你的問題。也許你有另一個問題? – 2014-10-06 14:24:26
***重要*** @Jacob在GitHub上發佈瞭解決此問題的解決方法:https://github.com/square/picasso/issues/691 – 2014-10-11 18:31:18