Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does calling requestLayout or invalidate multiple times hurt performance?

Tags:

android

To move a view around I call setX, setY, and some other functions that set width and height and call either invalidate or requestLayout at the end of each of these functions. As a result invalidate and requestLayout gets called multiple times per user event. Does this trigger multiple layout/draws per user event?

like image 327
jz87 Avatar asked Oct 23 '11 17:10

jz87


2 Answers

Of course not.We can see code in ViewRootImpl

    void invalidate() {
    mDirty.set(0, 0, mWidth, mHeight);
    if (!mWillDrawSoon) {
        scheduleTraversals();
    }
   }

invalidate set dirty area, and call scheduleTraversals,it will call doTraversal.

    void doTraversal() {
    if (mTraversalScheduled) {
        mTraversalScheduled = false;
        mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);

        if (mProfile) {
            Debug.startMethodTracing("ViewAncestor");
        }

        performTraversals();

        if (mProfile) {
            Debug.stopMethodTracing();
            mProfile = false;
        }
    }
}

we can see mTraversalScheduled flag in the code,if we are scheduleding travel, an invalidate events triggerd,mTraversalScheduled is false in doTraversal,so the method will return directly. So NOT every invalidate events called performTravel,NOT every invalidate events called redraw

like image 163
chefish Avatar answered Oct 11 '22 11:10

chefish


Yes, it does. Everytime you call invalidate, the view itself will redraw!

like image 30
DarkNeo Avatar answered Oct 11 '22 11:10

DarkNeo