-1

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?

Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
Rookie
  • 175
  • 1
  • 4
  • 19

1 Answers1

0

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

FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
  • can glide be used for loading images on device? – Rookie Jan 15 '18 at 19:19
  • yes, please read this [tutorials](https://futurestud.io/tutorials/glide-getting-started). glide has many methods for loading images, you can load images from url, file and from drawables. – FarshidABZ Jan 16 '18 at 07:49