Android将图像高效加载到imageview中 [英] Android load image into imageview efficiently

查看:118
本文介绍了Android将图像高效加载到imageview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have next problem: To avoid OutOfMemoryError i'm loading my big images into ImageViews using Picasso in such way:

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

在活动或片段中,我将图片加载到ImageView

And in activity or fragment, i'm loading image into ImageView

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

但是,我有两个问题:


  1. 图像加载有一些延迟(但我需要加载它)

  2. 在某些情况下,centerCrop()不是我需要的。

  1. Image loads with some delay (but i need to load it immideatly)
  2. centerCrop() is not what i need in some cases.

我如何实现topCrop()或bottomCrop()之类的东西?我试过 https://github.com/cesards/CropImageView 库,
但是我在setFrame()方法中获取NullPointerException(getDrawable()返回null),因为图像尚未加载。
提前致谢!

How can i implement something like topCrop() or bottomCrop()? I've tried https://github.com/cesards/CropImageView library, but i'm getting NullPointerException in setFrame() method (getDrawable() returns null), because image did not load yet. Thanks in advance!


推荐答案


图像加载有一些延迟(但我需要加载它)

Image loads with some delay (but i need to load it immideatly)

你必须选择:(a)内存有效延迟或(b)即时但可能的OOM。我说根据经验,我在Play商店的应用程序有一点延迟,而毕加索正在处理它。这就是它的工作原理。

you have to choose: (a) memory efficient delay or (b) instant but probable OOM. And I said that by experience, the app I have on the Play Store have a little delay while Picasso is processing it. It's just how it works.


在某些情况下,centerCrop()不是我需要的。

centerCrop() is not what i need in some cases.

然后你应该将图像加载到() a 目标 。目标将在UI线程中收到 Drawable ,从那里你可以在ImageView中设置它(使用 setScaleType )或任何你想要的背景。甚至可能使用 InsetDrawable 调整头寸。

then you should load the image into() a 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 to adjust position.

这篇关于Android将图像高效加载到imageview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆