I have helper methods that set the visibility of certain View
s depending on the state variables that are passed into the methods. Sometimes, these methods will get called many times and the View
s visibility will not change. So I found myself starting to check the visibility of each View
before setting it with the thinking, "No point in changing a View's visibility to the same visibility and causing a refresh for no reason".
if (myView.getVisibility() != View.VISIBLE) { myView.setVisibility(View.VISIBLE); } etc...
However, now I'm wondering if the implementation of setVisibility
already takes this into account, and checks if you are setting the same visibility to what the View
already has, and doesn't needlessly refresh the View
(what my code is trying to do).
So does anyone know if my "optimization" is actually improving any performance, or is the API already a step ahead of me?
They're already one step ahead. See the code for View.java:setVisibility()
:
public void setVisibility(int visibility) { setFlags(visibility, VISIBILITY_MASK); ... }
It calls setFlags()
:
void setFlags(int flags, int mask) { int old = mViewFlags; mViewFlags = (mViewFlags & ~mask) | (flags & mask); int changed = mViewFlags ^ old; if (changed == 0) { return; } .... }
It checks to see if the flag matches the current state. If so, it simply returns without doing anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With