gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:14 25 4
gpt4 key购买 nike

我的 Activity 中有一个固定的 header ,我需要将图像动态加载到其中。我需要这些图像来完全填充此标题,并保持宽度和高度比例。

如果我之前加载图像,我会得到 OutOfMemoryException。如果我使用 picasso 的 fit() 方法,该区域将出现空白。如果我像 StackOverflow 中的一些人所建议的那样使用 Transformation,我也会得到 OutOfMemoryException。

我该如何解决这个问题?

header 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º 的图像时,Picasso 计算错误了尺寸。

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

我认为问题出在 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;
}

最佳答案

使用 .fit().centerCrop(),它应该可以满足您的需求。

关于android - 设置最大宽度以使用 Picasso 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26208266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com