2

I have a RecyclerView and when I click on item it should display an imageSlider with some images(just 2 or 3). Everything works fine but there's a little delay on click, something it takes 2 seconds which is very annoying? How can I fix it?

This is the slider code that load images from an array of bitmaps:

public class ImageAdapter extends PagerAdapter {

    private Context context;
    private ArrayList<Bitmap> bitmaps;

    public ImageAdapter(Context context, ArrayList<Bitmap> bitmaps) {
        this.context = context;
        this.bitmaps = bitmaps;
    }

    @Override
    public int getCount() {
        return bitmaps.size();
    }


    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }



    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((ImageView) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        ImageView imageView = new ImageView(context);
        Glide.with(container)
                .asBitmap()
                .centerCrop()
                .load(bitmaps.get(position))
                .into(imageView);

        container.addView(imageView,0);
        return imageView;

    }
}
sourav.bh
  • 467
  • 1
  • 7
  • 20
  • 1
    you are loading a few images on Click. the delay is normal. what you should do is to bring down bitmap sizes. whatever you can do to make loading time faster. Don't load them Before the Click Though, let it load after the click – Shahin Mar 30 '19 at 12:15
  • @Shahin thanks for your answer. How can i do to bring down bitmap sizes? – Filippo Schianchi Mar 30 '19 at 13:38
  • There are so many ways to Scale down bitmap. You can use Glide Library. Or you can use androids built-in Bitmap tools which I'll put it in an answer for u rn – Shahin Mar 30 '19 at 13:46

1 Answers1

0

Bitmap Scaling:

Bitmap bitmap = Bitmap.createScaledBitmap(yourBitmap, 100, 100, false);

Bitmap bitmap = Bitmap.createBitmap(yourBitmap, width, height, Bitmap.Config.ARGB_8888);

Or with Glide:

Glide.with(context).asBitmap().load(yourBitmap).apply(new RequestOptions().placeholder(placeHolderDrawable).error(errorDrawable).override(width, height)).into(yourImageView);
Shahin
  • 391
  • 3
  • 9