Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid onMeasure and onLayout everytime ImageView Bitmap is set

This is my setup. I have a custom ListView with some fancy headers, which is inside a ViewPager. I'm maintaining a memory cache of images that having been downloaded and stored locally like any self-respecting developer would.

The issue I'm having is that everytime I call setImageBitmap(Bitmap bm) on the ImageView in my ListView's getView() method, using a Bitmap from the cache, it's causing my all Views to onMeasure(). I've looked through the Android ImageView source code and found this:

public void setImageDrawable(Drawable drawable) {
    if (mDrawable != drawable) {
        mResource = 0;
        mUri = null;

        int oldWidth = mDrawableWidth;
        int oldHeight = mDrawableHeight;

        updateDrawable(drawable);

        if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
            requestLayout();
        }
        invalidate();
    }
}

So I can see that setImageDrawable (which is called by setImageBitmap) requests its layout whenever the drawable has differing dimensions than its previous Drawable. The problem is that my ImageView is set static to 60dp x 60dp, so its size isn't going to change regardless of the size of its image.

Is there a way I can get around the layouts being constantly remeasured as I scroll? The onMeasure and onLayout calls are propagating their way up the View tree, so my ListView and ViewPager are also getting measured in the middle of the list scrolling, which is obviously causing serious performance issues (~100ms UI jumps).

The performance is awful and I need to correct it. Anyone have any solutions?

Thanks.

like image 275
jmhend Avatar asked Sep 21 '12 01:09

jmhend


1 Answers

It has been a while but I have a similar issue recently, one row in my listview's height changed too much, causing the listview.onmeasure called.

If you are build against API-14 and above, you can set breakpoint in the SDK source code. that should help to figure out why height is changed.

like image 195
cheng yang Avatar answered Oct 14 '22 21:10

cheng yang