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

weixin_38092995 2019-09-12 11:46:24
我的活动中有一个固定的标题,我需要动态加载图像.我需要这个图像来完全填充这个标题,保持宽度和高度的比例. 如果我之前加载图像,我会得到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); UPDATE 当我加载exif orientation =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; }
...全文
74 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38097015 2019-09-12
  • 打赏
  • 举报
回复
使用.fit().centerCrop(),它应该做你想要的.

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧