1

First of all, yes, I'm aware of this question, but the answer doesn't help me. Yes, it solves the "changing position when scrolling" issue but it's not a very optimal solution to my problem. Let me explain...

So, this is my getView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    UnscaledImageView imageView;

    if(convertView == null) {
        imageView = new UnscaledImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageResource(mThumbIds[position]);
    } else {
        imageView = (UnscaledImageView)convertView;
    }

    return imageView;
}

And the common solution would be to move imageView.setImageResource(mThumbIds[position]); outside and below the if statement. But this creates another problem...

As you may have noticed I'm using a UnscaledImageView instead of ImageView. This is a custom subclass of ImageView where I've overridden the setImageResource() method to manually decode bitmaps with some extra options to fit my needs. The manual decoding is what causes my problem.

If the setImageResource() method stays inside the if, no problem, the bitmap decoding will be done once for bitmap (during GridView loading) and the scrolling will be smooth... But the images will be jumping to different positions with each scroll.

But if the setImageResource() method stays outside the if, the images will not jump to different positions but all images will be constantly decoded with each scroll. The scrolling will be jerky and not smooth at all.

How can I solve this issue? Any ideas?

Community
  • 1
  • 1
rfgamaral
  • 16,546
  • 57
  • 163
  • 275

1 Answers1

1

Maybe you could use some kind of buffering or caching for your decoded images. Your setImageResource method should check if its input image is already in the buffer (i.e. if it has been already decoded). If so then your method simply would retrieve the decoded image from the buffer. If not, then it would decode the image and store it in the buffer for future uses. How to implement the buffer/cache depends on your own needs. I'm sure there are tons of possibilities. This way you could move the call of setImageResource out of the if-then block and wouldn't have to decode the image everytime the getView method is called.

Vicent
  • 5,322
  • 2
  • 28
  • 36
  • Maybe something like this http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html? – rfgamaral Aug 27 '12 at 22:17
  • Yes, that is a standard way of catching bitmaps on android. But the exact implementation its up to you and depends on the needs of your application. You may need something with a complex implementation but very efficient, or something with an easy implementation (like an array of bitmaps) but not so efficient. Lrucache seems a good starting point. In fact, I'm going to try it on my own app :-) – Vicent Aug 28 '12 at 07:13