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

标签 android bitmap imageview out-of-memory

我有下一个问题: 为了避免 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);

但是,我有两个问题:

  1. 图片加载有一些延迟(但我需要立即加载)
  2. 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/

相关文章:

android - 降低android中位图的DPI分辨率

java - 更改图像宽度和高度

Android imageView id 不显示

java - android: 自定义 ImageView onDraw 方法无法在 y 轴上正确绘制

android - 具有右对齐、裁剪、未缩放内容的 ImageView

android - Kotlin Android 扩展访问 TextView

android - 无效的命令行参数 : dns-server

android - 服务器更新设备未使用

android - getActivity().getActionBar() 在 fragment 上给出 null

java - 试图缩小 Android 中的位图不起作用