I am following the tutorial in this link to learn about RecyclerView. The tutorial loads images from drawable folder. When I do the same the app just runs out of memory. So I made a little change to resize images.

@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
    viewHolder.title.setText(galleryList.get(i).getImage_title());
    viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
    viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));
}

Changed the 3rd line to this

viewHolder.img.setImageDrawable(scaleImage(galleryList.get(i).getImage_ID(), ScaleFactor));

And defined scaleImage as

private Drawable scaleImage(int imageId, float scaleFactor)
{
    Drawable image = context.getResources().getDrawable(imageId);

    if ((image == null) || !(image instanceof BitmapDrawable)) {
        return image;
    }

    Bitmap b = ((BitmapDrawable)image).getBitmap();

    int sizeX = Math.round(image.getIntrinsicWidth() * scaleFactor);
    int sizeY = Math.round(image.getIntrinsicHeight() * scaleFactor);

    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, false);

    image = new BitmapDrawable(context.getResources(), bitmapResized);

    return image;
}

But as I scroll RecyclerView lags until It loads the coming rows which I think is because I load each image individually and then resize it. Is there a way to do this asychronously?

1 Answers 11

up vote 0 down vote accepted

It's become cause you are resizing the image, it's a heavy process. try to resize images async or use image loader libs.

There are some useful libraries you can use for loading images like Glide

Also, you can see this Picasso v/s Imageloader v/s Fresco vs Glide

upvote
  flag
can glide be used for loading images on device? – Halil Karagöz
upvote
  flag
yes, please read this tutorials. glide has many methods for loading images, you can load images from url, file and from drawables. – FarshidABZ

Not the answer you're looking for? Browse other questions tagged or ask your own question.