【发布时间】:2015-05-03 06:54:57
【问题描述】:
我正在尝试以全屏视图显示图像并使用以下代码:
// Target to write the image to local storage.
Target target = new Target() {
// Target implementation.
}
// (1) Download and save the image locally.
Picasso.with(context)
.load(url)
.into(target);
// (2) Use the cached version of the image and load the ImageView.
Picasso.with(context)
.load(url)
.into(imgDisplay);
此代码在较新的手机上运行良好,但在具有 32MB 虚拟机的手机上,我遇到了内存不足的问题。所以我尝试将(2)更改为:
Picasso.with(context)
.load(url)
.fit()
.into(imgDisplay);
这导致图像失真。由于在下载图像之前我不知道图像的尺寸,因此我无法设置 ImageView 尺寸,因此在调整图像大小时不考虑我的 ImageView 中的纵横比:
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
处理这种情况的最佳方法是什么?我的原始图像的最大宽度为 768,高度为 1024,当屏幕比这小得多时,我想进行下采样。如果我尝试使用resize(),我的代码会变得复杂,因为我必须等待(1)完成下载,然后在(2)中添加resize()。
我假设转换在这种情况下没有帮助,因为public Bitmap transform(Bitmap source) 的输入已经有很大的位图,这会导致我内存不足。
【问题讨论】:
标签: android-image picasso