Android View可见性判断方法

在Android开发过程中,经常需要获取Window或某个View的可见性变化时机,以便在View的Visibility变化时进行相应的处理。目前,比较常用的判断View可见性时机的回调有

  • onWindowVisibilityChanged
  • onVisibilityChanged
  • OnAttachStateChangeListener#onViewAttachedToWindow

一、onWindowVisibilityChanged

 		/**
     * Called when the window containing has change its visibility
     * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}).  Note
     * that this tells you whether or not your window is being made visible
     * to the window manager; this does <em>not</em> tell you whether or not
     * your window is obscured by other windows on the screen, even if it
     * is itself visible.
     *
     * @param visibility The new visibility of the window.
     */
		protected void onWindowVisibilityChanged(@Visibility int visibility) {
   
        if (visibility == VISIBLE) {
   
            initialAwakenScrollBars();
        }
    }

由方法注释可知,它是在窗口可见性改变时调用,而且注意这只是在Window对WindowManager可见时调用,并不是告知你当前可见的Window是否被遮挡。

查看代码,发现其调用位置有3处,添加时在performTraversals方法中(代码有省略),Activity onStop生命周期会removeDecorView和对应的Window,在removeView方法中会调用dispatchDetachedFromWindow方法,该方法内又会调用onWindowVisibilityChanged

private void performTraversals() {
   
        // cache mView since it is used so much below...
        final View host = mView;
				......

        final int viewVisibility = getHostVisibility();
        final boolean viewVisibilityChanged = !mFirst
                && (mViewVisibility != viewVisibility || mNewSurfaceNeeded
                // Also check for possible double visibility update, which will make current
                // viewVisibility value equal to mViewVisibility and we may miss it.
                || mAppVisibilityChanged);
        mAppVisibilityChanged = false;
        final boolean viewUserVisibilityChanged = !mFirst &&
                ((mViewVisibility == View.VISIBLE) != (viewVisibility == View.VISIBLE));

        ......
        if (mFirst) {
   
            ......

            // We used to use the following condition to choose 32 bits drawing caches:
            // PixelFormat.hasAlpha(lp.format) || lp.format == PixelFormat.RGBX_8888
            // However, windows are now always 32 bits by default, so choose 32 bits
            mAttachInfo.mUse32BitDrawingCache = true;
            mAttachInfo.mWindowVisibility = viewVisibility;
            mAttachInfo.mRecomputeGlobalAttributes = false;
            mLastConfigurationFromResources.setTo(config);
            mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
            // Set the layout direction if it has not been set before (inherit is the default)
            if (mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
   
                host.setLayoutDirection(config.getLayoutDirection());
            }
           /**
            *	①
            */
            host.dispatchAttachedToWindow(mAttachInfo, 0);
            mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(true);
            dispatchApplyInsets(host);
        } else {
   
           	......
            }
        }

        if (viewVisibilityChanged) {
   
            mAttachInfo.mWindowVisibility = viewVisibility;
           /**
            *	②
            */
            host.dispatchWindowVisibilityChanged(viewVisibility);
            if (viewUserVisibilityChanged) {
   
                host.dispatchVisibilityAggregated(viewVisibility == View.VISIBLE);
            
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值