0

Base on the input data, my layout will show some view and hide some other. So should I check their visibility with getVisibility() before setVisibility()?

What would be better in term of delay time performance?

(My layouts have a lot of picture and it makes the delay time between activities quite long. So this is reason why I post this question)

GrizzlyManBear
  • 647
  • 8
  • 16
Anh-Tuan Mai
  • 1,129
  • 19
  • 36

1 Answers1

1

Always check the source. In this instance View.java

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/View.java#L7424-L7425

public void setVisibility(@Visibility int visibility) {
    setFlags(visibility, VISIBILITY_MASK);
}

Calls setFlags :

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/View.java#L10538

This checks if the flag has changed, if it hasn't it returns quickly:

    ...
    int old = mViewFlags;
    mViewFlags = (mViewFlags & ~mask) | (flags & mask);

    int changed = mViewFlags ^ old;
    if (changed == 0) {
        return;
    }
    ...

So although I haven't got numbers for you in terms of performance, I would say the difference is negligible because it will not change if you set it to its already current value.

Also contrary to that getVisibility doesn't do much either:

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/View.java#L7413-L7415

public int getVisibility() {
    return mViewFlags & VISIBILITY_MASK;
}
Blundell
  • 75,855
  • 30
  • 208
  • 233