Android load image into imageview efficiently

允我心安 提交于 2019-12-02 09:34:59

问题


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();
}

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);

However, i have two problems:

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

    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!


回答1:


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

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() is not what i need in some cases.

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.



来源:https://stackoverflow.com/questions/30823326/android-load-image-into-imageview-efficiently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!