view#post和Handler#psot区别

        v.post {  }
        Handler().post {  }
View#post方法源码
  1. 业务场景:当我们在activity#onCreate或是在fragment#onViewCreated方法中直接获取view的宽高为0,为什么?

根据View视图的绘制流程,view的宽高只有在测量之后才能获取到,而view的测量是在activityThread的handleResumeActivity方法中,通过创建ViewRootImp,把DecoreView和ConetentView连接起来,调用perfromTravels(),进而完成view的测量布局和绘制。所以获取的值为0。

  1. 如何获取到宽高?
 view.post { //在这个方法内部就能获取到宽高 }

跟进view#post源码进一步分析

 public boolean post(Runnable action) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().post(action);
        return true;
    }

我们发现当attachInfo != null时候,会通过handler把消息加入消息队列中执行,但是当这个值为null,会把这个Runnable加入到一个数组队列中。等待执行。

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

        synchronized (this) {
            if (mActions == null) {
                mActions = new HandlerAction[4];
            }
            mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
            mCount++;
        }
    }
  1. 什么时候执行存储在数组中的Runnable呢?

ViewRootImp#perfromTravels(),在View测量布局绘制之后执行,所以在view#post中能获取宽高值

   //开始调用handler处理view在没有mAttachInfo的情况下,view.post会存runnable任务到一个数组中,在此时通过handler去执行,加入消息队列中去
        getRunQueue().executeActions(mAttachInfo.mHandler);
Handler().post
  1. handler#post和View#post有什么区别?

看源码实现,直接把Runnable加入到消息队列中,等待执行。和view#post相比,其实就是多了个attachInfo判断,view中如果attachInfo != null,同样是调用了handler#post方法,同样的逻辑。

/**
     * Causes the Runnable r to be added to the message queue.
     * The runnable will be run on the thread to which this handler is 
     * attached. 
     *  
     * @param r The Runnable that will be executed.
     * 
     * @return Returns true if the Runnable was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean post(@NonNull Runnable r) {
       return  sendMessageDelayed(getPostMessage(r), 0);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌晨三点的北京

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值