【自定义View】为什么View.post可以拿得到宽高

view.post

    public boolean post(Runnable action) {
        //这个mAttachInfo只有在dispatchAttachedToWindow被赋
        //值,所以这里假设还没绘制完成
        final AttachInfo attachInfo = mAttachInfo;
        //这里是已经绘制完成了
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }
        
        getRunQueue().post(action);
        return true;
    }

HandlerActionQueue.post

    public void post(Runnable action) {
        postDelayed(action, 0);
    }



    public void postDelayed(Runnable action, long delayMillis) {
        final HandlerAction handlerAction = new HandlerAction(action, delayMillis);

        synchronized (this) {
            if (mActions == null) {
                mActions = new HandlerAction[4];
            }
            //只是单纯地将任务添加进去,并没有post出去,post
            //的时机在executeAction里面
            mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
            mCount++;
        }
    }

executeActions

    //调用executeActions的方法为dispatchAttachedToWindow
    public void executeActions(Handler handler) {
        synchronized (this) {
            final HandlerAction[] actions = mActions;
            for (int i = 0, count = mCount; i < count; i++) {
                final HandlerAction handlerAction = actions[i];
                //这里真正将任务post出去
                handler.postDelayed(handlerAction.action, handlerAction.delay);
            }

            mActions = null;
            mCount = 0;
        }
    }

dispatchAttachedToWindow

    //调用dispatchAttachedToWindow的方法在viewrootImpl的
    //performTraversals
    void dispatchAttachedToWindow(AttachInfo info, int visibility) {
        mAttachInfo = info;
        if (mOverlay != null) {
            mOverlay.getOverlayView().dispatchAttachedToWindow(info, visibility);
        }
        mWindowAttachCount++;
        mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
        if (mFloatingTreeObserver != null) {
            info.mTreeObserver.merge(mFloatingTreeObserver);
            mFloatingTreeObserver = null;
        }

        registerPendingFrameMetricsObservers();

        if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
            mAttachInfo.mScrollContainers.add(this);
            mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
        }
        //将任务发出去
        if (mRunQueue != null) {
            mRunQueue.executeActions(info.mHandler);
            mRunQueue = null;
        }

        //...
    }

performTraversals

    private void performTraversals() {
        //在这里将消息添加到任务队列里面
        //但是!
        //performTraversals本来也是一个runnable任务来的
        //所以最快也得等这个方法执行完才会执行view.post
        host.dispatchAttachedToWindow(mAttachInfo, 0);
        //回调windowAttach??
        mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(true);  

        performMeasure();
        performLayout()
        performDraw();          
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值