Android load image into imageview efficiently

942 views Asked by At

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

There are 1 answers

0
Budius On BEST ANSWER

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.