Android 有效地将图像加载到图像视图中

Android load image into imageview efficiently

我有下一个问题: 为了避免 OutOfMemoryError,我以这种方式使用 Picasso 将我的大图像加载到 ImageViews 中:

public static RequestCreator picasso(final Context context, final int resourceId) {
    return Picasso.with(context)
            .load(resourceId)
            .fit()
            .centerCrop()
            .noFade();
}

并且在 activity 或片段中,我正在将图像加载到 ImageView

final ImageView backgroundImage = (ImageView) root.findViewById(R.id.background_image);
Util.picasso(getActivity(),R.drawable.background_soulmap)
            .into(backgroundImage);

但是,我有两个问题:

  1. 图片加载有一些延迟(但我需要立即加载)
  2. centerCrop() 在某些情况下不是我需要的。

    如何实现类似 topCrop() 或 bottomCrop() 的功能?我试过 https://github.com/cesards/CropImageView 库, 但是我在 setFrame() 方法中得到 NullPointerException (getDrawable() returns 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() 一个 Target. The target will receive the Drawable in the UI thread, from there you can set it up inside the ImageView (using the setScaleType) or as a background anyway you want. Maybe even using an InsetDrawable 来调整位置。