我有下一个问题: 为了避免 OutOfMemoryError,我以这种方式使用 Picasso 将我的大图像加载到 ImageViews 中:
public static RequestCreator picasso(final Context context, final int resourceId) {
return Picasso.with(context)
.load(resourceId)
.fit()
.centerCrop()
.noFade();
}
在 Activity 或 fragment 中,我正在将图像加载到 ImageView 中
final ImageView backgroundImage = (ImageView) root.findViewById(R.id.background_image);
Util.picasso(getActivity(),R.drawable.background_soulmap)
.into(backgroundImage);
但是,我有两个问题:
- 图片加载有一些延迟(但我需要立即加载)
centerCrop() 在某些情况下不是我需要的。
我如何实现类似 topCrop() 或 bottomCrop() 的东西?我试过了 https://github.com/cesards/CropImageView图书馆, 但是我在 setFrame() 方法中得到 NullPointerException(getDrawable() 返回 null),因为图像还没有加载。 提前致谢!
最佳答案
Image loads with some delay (but i need to load it immideatly)
您必须选择:(a) 内存高效延迟或 (b) 即时但可能的 OOM。我说过,根据经验,我在 Play Store 上的应用程序在 Picasso 处理它时会有一点延迟。这就是它的工作原理。
centerCrop() is not what i need in some cases.
那么你应该加载图像 into()
a Target
.目标将在 UI 线程中接收 Drawable
,您可以从那里在 ImageView 中设置它(使用 setScaleType
)或将其设置为任何您想要的背景。甚至可能使用 InsetDrawable
调整位置。
关于Android有效地将图像加载到imageview中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823326/