美文网首页
GONE和INVIESIABLE的区别

GONE和INVIESIABLE的区别

作者: 风月寒 | 来源:发表于2021-06-06 19:35 被阅读0次

GONE和INVIESIABLE的最直观的区别:

GONE不显示而且不占位置的,而INVISIBLE是不显示但是占位置的。

if ((changed & GONE) != 0) {
            needGlobalAttributesUpdate(false);
            requestLayout();

            if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
                if (hasFocus()) {
                    clearFocus();
                    if (mParent instanceof ViewGroup) {
                        ((ViewGroup) mParent).clearFocusedInCluster();
                    }
                }
                clearAccessibilityFocus();
                destroyDrawingCache();
                if (mParent instanceof View) {
                    // GONE views noop invalidation, so invalidate the parent
                    ((View) mParent).invalidate(true);
                }
                
                mPrivateFlags |= PFLAG_DRAWN;
            }
            if (mAttachInfo != null) {
                mAttachInfo.mViewVisibilityChanged = true;
            }
        }

        /* Check if the VISIBLE bit has changed */
        if ((changed & INVISIBLE) != 0) {
            needGlobalAttributesUpdate(false);
            mPrivateFlags |= PFLAG_DRAWN;

            if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE)) {
                if (getRootView() != this) {
                    if (hasFocus()) {
                        clearFocus();
                        if (mParent instanceof ViewGroup) {
                            ((ViewGroup) mParent).clearFocusedInCluster();
                        }
                    }
                    clearAccessibilityFocus();
                }
            }
            if (mAttachInfo != null) {
                mAttachInfo.mViewVisibilityChanged = true;
            }
        }

而从源码可以发现,设置为GONE时,会调用((View) mParent).invalidate(true),相当于会重绘。而设置成INVISIBLE则会少走重绘代码。从视图变更开销的来说INVISIBLE要更加的划算一些,如果你的View不是十分占用资源的情况。

相关文章

网友评论

      本文标题:GONE和INVIESIABLE的区别

      本文链接:https://www.haomeiwen.com/subject/yzvqeltx.html