4

In my app I have a GridView with many images (~100), layout similar to this one:

I'm testing my app on a HTC Desire HD. When I have 88 images in the gridview, the scrolling is smooth. Now, if I add 10 images, so I have 98 in total, there isn't anything close to smooth scrolling anymore - it's now "slow", more rough, it's like it's scrolling in big chunks.

This gridview is present in a TabLayout, in which I also have a ListView with many elements. When using 98 images in the gridview, also scrolling in this listview is affected. So it's like the whole application gets slower somehow (or uses more "power" than the phone can manage).

Is there any way I can optimize the gridview so it does not use so much power?

(PS: when using about 150 images, the application simply stops working (it disconnects, and no error is shown in the log/debug).)

Tushar
  • 8,019
  • 31
  • 38
gosr
  • 4,593
  • 9
  • 46
  • 82

4 Answers4

4

Make sure you are recycling your images in the getView() method of the adapter. Something like this:

public View getView(int position, View convertView, ViewGroup parent) {

        // if it's not recycled, initialize some attributes
        if (convertView == null) {

            convertView = new View(...);

        }
...

instead of making new Views each time getView() is called.

Luis Ollero
  • 383
  • 1
  • 2
  • 8
  • Now that I have access to your code I will suggest you to stop storing all the Bitmaps on the array. If the images are resources you can just store the id's of the drawables and, if they are files in the sdcard, it's better to keep in memory the paths. In either way you can load dynamically the ImageViews. – Luis Ollero Aug 04 '11 at 08:00
2

In the end I went with the solution provided by NOSTRA here:

Lazy load of images in ListView

It works really well.

Community
  • 1
  • 1
gosr
  • 4,593
  • 9
  • 46
  • 82
1

I don't code in Android but I'd suggest a lazy visibility approach. All your images appear to be the same size, so handle the scroll event and show only the images that can be seen on the current view frustum (the screen) while hiding the the rest.

My guess is, Android is using its processing power on the images that can't be seen during scrolling which causes the jitters.

LastCoder
  • 194
  • 2
0

Firstly, do not keep all your bitmaps in memory private Bitmap[] mBitmap;. That's probably what crashes your app. Read them from the filesystem with BitmapFactory.

Secondly, you should not scale your images in your adapter, that is expensive:

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

Try with a set of images that do not need to be scaled.

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • Thanks @Xavi. Just tried to comment that line with the scaling - no difference. I'm not sure how to use BitmapFactory. I have a for-loop in which I get the drawable from the resources; then I save it in a Drawable array, which I, lastly, convert to a Bitmap array: `bitmapArray[k+q] = ((BitmapDrawable)imageArray[k+q]).getBitmap();`. Do you have any suggestions on how to use BitmapFactory for the same thing? – gosr Aug 03 '11 at 21:23
  • @eightx2: Yes, the scaling is not your problem, but I read Romain Guy saying that it is expensive. So keep it in mind for the future. Regarding the Bitmap issue, if I'm not wrong you could do something like `imageView.setImageBitmap( BitmapFactory.decodeResource( mContext.getResources(), R.drawable.whatever ));` instead of `imageView.setImageBitmap(mBitmap[position]);` (haven't actually checked that the code is right). – Xavi Gil Aug 03 '11 at 21:38
  • Cheers. However, I don't see how this should fix the memory problem; as you can see, `mBitmap` is created in the constructor, therefore, when I call `new ReleasesImgAdapter` from another activity, it must be in the memory somehow? – gosr Aug 03 '11 at 21:51
  • One more thing regarding the scaling issue. Removing the line doesn't mean the image doesn't scale. If your images are bigger in size than your ImageView area, they could be resizing as well (depends on the default ImageView `ScaleType`). – Xavi Gil Aug 03 '11 at 22:03
  • Changing the `imageView.setImageBitmap` line, there is no need to load all the images in memory anywhere. Change your constructor and remove that parameter. – Xavi Gil Aug 03 '11 at 22:11
  • Thanks, just now I've tried to store all the drawable IDs in an integer array and using `imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), mIDs[position]));` instead. So now I do not have any bitmaps in memory. However, the scrolling issue has not been fixed by this, but I can say that the listview mentioned in the first post is now _not_ affected by the scrolling issue. So I don't know - maybe Android doesn't like so many items in a gridview? – gosr Aug 05 '11 at 16:31
  • Glad you are making little progress :) Does your app crash with 150 images now? If it does, can you post the logcat, please? If it doesn't, what are you images size and dimension? – Xavi Gil Aug 05 '11 at 17:53
  • No, it does not crash now with 150 images. All my images are stored in the "drawable" folder (I plan to make different sizes for different screen densities later). Dimensions are 180x180, and the average size is about 50KB. – gosr Aug 05 '11 at 20:03
  • Right, I think your images are still being scaled each time they are set to the view. Their dimensions (180x180) are bigger than your biggest layout (140x140 in hdpi). Try to downsize them to a value under the layout dimensions for each density. Be sure there is no scaling. Hope this will help. – Xavi Gil Aug 05 '11 at 20:42
  • Now I've downsized all images to 140x140 (and tried with layoutparams for both 140x140 and a bigger one, 145x145 for instance (testing on a phone which is hdpi)), but unfortunately the scrolling issue still persists. And no scaling is present. – gosr Aug 05 '11 at 22:05
  • Everything in your code seems to be fine. Would lazy loading be a good solution in your case? If it would, have a look [here](http://blog.jamesbaca.net/?p=67) and [here](http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012). – Xavi Gil Aug 08 '11 at 18:07