Does the request cause requestLayout or invalidate multiple performance corruptions?

To move the view around, I call setX, setY, and some other functions that specify the width and height and call either invalidate or requestLayout at the end of each of these functions. As a result, invalidate and requestLayout are called several times for each user event. Does this trigger multiple layouts / drawings on a user event?

+4
source share
3 answers

Of course not. We can see the code in ViewRootImpl

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

nullify the 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 the mTraversalScheduled flag in the code, if we plan to move, invalidate events triggerd, mTraversalScheduled is false in doTraversal, so the method will return directly. Therefore, NOT every invalid event called performTravel, NOT every invalid event called redraw

+3
source

Yes Yes. Every time you call invalid, the view itself will be redrawn!

+1
source

Every time you call invalidate, the view itself will be redrawn!

This is not technically true. invalidate set a flag in the view, which makes it suitable for redrawing.

A simple test would be to put the log statement in onDraw and then call invalidate in a loop. You will see that there is no log for each iteration.

+1
source

All Articles