关于View.post()/postDelay()方法的一些分析

一.引言

经常在Android的java代码中动态设置布局的读者应该会对动态获取控件宽高不陌生,在最近项目中我也有用到。我们知道直接在onCreate方法中无法通过getWidthgetHeight获取到想要的控件的宽高具体值。因此有几种方式来获取,例如在监听中获取或者通过View.post的方式获取。具体方法可以参考下面的这篇文章,写得比较仔细。
Activity启动过程中获取组件宽高的五种方式
笔者也有使用View.post来获取,不过始终对这个方法心存疑问,为什么一个类似handler.post的方法调用之后就可以正确得到宽高?通过查看源码以及查阅的一些资料大致弄懂了流程,接下来说一下我的分析,如果有不对的地方欢迎指教~

二.View.post方法的调用机制

View的post和postDelay方法其实是类似的,我们点进View.java中的这两个方法看一下:

  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;
    }
   ...
   public boolean postDelayed(Runnable action, long delayMillis) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.postDelayed(action, delayMillis);
        }

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

这两个方法中的代码基本是一致的,不同的只是postDelay中有将delayMillis传进去,而在post中最终也是调用了postDelay这个方法,只是将delayMillis置为0了,我们可以点进getRunQueue().post()方法中看看:

getRunQueue().post()

可以看到确实如此。
接下来分析post中具体代码。代码比较简洁,我们可以看到其中有对mAttachInfo赋值的attachInfo进行判断,如果为空,则调用getRunQueue().post(),否则直接返回attachInfo.mHandler.post(action)。那么这两个post有什么区别?

1.先来看当mAttachInfo不为null时的情况,因为这个较为简单,点进attachInfo.mHandler.post发现其实就是调用的Handler.post()。这里就要看看这里的mHandler是哪里产生的。使用AS的ctrl+左键一直追根溯源:

final static class AttachInfo {
...
   final Handler mHandler;
...
   AttachInfo(IWindowSession session, IWindow window, Display display,
                ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer,
                Context context) {
            mSession = session;
            mWindow = window;
            mWindowToken = window.asBinder();
            mDisplay = display;
            mViewRootImpl = viewRootImpl;
            mHandler = handler;
            mRootCallbacks = effectPlayer;
            mTreeObserver = new ViewTreeObserver(context);
        }

可以看到mHandler是AttachInfo的一个变量,在AttchInfo的构造方法中被赋值,查看这个构造方法的调用点(这里可以结合Source Insight的ctrl+/的查找快捷键进行查找):

public final class ViewRootImpl implements ViewParent,
        View.AttachInfo.Callbacks, ThreadedRenderer.DrawCallbacks {
...
        mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this,
                context);

可以看到是在ViewRootImpl方法中将mHandler传入,查看mHanlder被赋值的地方:

  final ViewRootHandler mHandler = new ViewRootHandler();

ViewRootImpl是在主线程中被创建,因此这个handler对象是主线程的handler,至此我们可以知道mAttachInfo不为空的时候其实是直接调用了主线程handler来处理我们post的消息。当然这里有个问题是为什么使用了主线程的handler来处理消息之后就可以获取正确的宽高呢?先别急,我们继续梳理了之后来解释这个问题~

2.上面已经分析了mAttachInfo不为空的情况,当mAttachInfo为空时,会调用getRunQueue()这个方法:

    private HandlerActionQueue getRunQueue() {
        if (mRunQueue == null) {
            mRunQueue = new HandlerActionQueue();
        }
        return mRunQueue;
    }

返回了一个HandlerActionQueue的实例。这个HandlerActionQueue是什么,点进去看看:

public class HandlerActionQueue {
    private HandlerAction[] mActions;
    private int mCount;

    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];
            }
            mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
            mCount++;
        }
    }
...
    private static class HandlerAction {
        final Runnable action;
        final long delay;

        public HandlerAction(Runnable action, long delay) {
            this.action = action;
            this.delay = delay;
        }

        public boolean matches(Runnable otherAction) {
            return otherAction == null && action == null
                    || action != null && action.equals(otherAction);
        }
    }
}

这个类包含了两个全局变量,一个应该是用来计数,另一个mActions则是一个HandlerAction数组,HandlerAction中封装了一个Runnable对象和一个延时delay
继续看,当调用了post之后,实际只是生成了一个默认长度为4的HandlerAction数组,将要实现的Runnable传入。那我们什么时候使用Runnable呢?我们可以注意到这个类中有个executeActions方法:

 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];
                handler.postDelayed(handlerAction.action, handlerAction.delay);
            }

            mActions = null;
            mCount = 0;
        }
    }

这个方法就是将我们post进来的Runnable使用handler进行处理,可以查看一下这个方法在哪里被调用以及handler是来自哪里。在View中可以查找到该方法在View.java类中的dispatchAttachedToWindow方法中被调用:

void dispatchAttachedToWindow(AttachInfo info, int visibility) {
        ...
        if (mRunQueue != null) {
            mRunQueue.executeActions(info.mHandler);
            mRunQueue = null;
        }
        ...
}

这里可以看到传入的就是AttachInfo 的mHandler变量,上面我们分析过最终mHandler就是主线程的Handler,那么现在的问题就是这个info是从哪里传入的。继续追查dispatchAttachedToWindow方法使用的地方(Source Insight):可以看到总共有四个类有调用这个方法:AttachInfo_AccessorViewViewGroupViewRootImpl

image.png

其中AttachInfo_Accessor.java类中没有我们需要关注的地方,View.java就是当前类,显然没有被调用的地方,进入ViewGroup.java看看:

 @Override
    void dispatchAttachedToWindow(AttachInfo info, int visibility) {
        mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
        super.dispatchAttachedToWindow(info, visibility);
        mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;

        final int count = mChildrenCount;
        final View[] children = mChildren;
        for (int i = 0; i < count; i++) {
            final View child = children[i];
            child.dispatchAttachedToWindow(info,
                    combineVisibility(visibility, child.getVisibility()));
        }
        final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
        for (int i = 0; i < transientCount; ++i) {
            View view = mTransientViews.get(i);
            view.dispatchAttachedToWindow(info,
                    combineVisibility(visibility, view.getVisibility()));
        }
    }

可以看到ViewGroupdispatchAttachedToWindow方法中,调用了ViewdispatchAttachedToWindow方法,将父中的AttachInfo全部传入子类child中实现赋值,而此时的child就是View类,我们不是要查看View类中的这个方法在哪里调用吗?这里貌似陷入死循环了。。没事,我们在看最后的ViewRootImpl方法中有没有什么信息:

private void performTraversals() {
  ...
  host.dispatchAttachedToWindow(mAttachInfo, 0);
  ...
  performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
  ...
  performLayout(lp, mWidth, mHeight);
  ...
  performDraw();
  ...
}

performTraversals方法中我们看到了这个方法。host是由mView赋值,而mView就是由顶层视图DecorView所赋值的。这个performTraversals方法就是用来调用测量、布局、绘制方法的地方。mAttachInfo唯一赋值的地方上面我们也有分析过:

mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this,
                context);

因此到这里我们就可以梳理一下了:在Activity的DecorView中调用dispatchAttachedToWindow时,将mAttachInfo传入到View中,并调用mRunQueue.executeActions的方法,该方法使用mAttachInfohandler将我们最初调用View.post(runnable)方法post进来的消息post到消息队列中进行处理,并且我们知道mAttachInfohandler是主线程的handler,因此其实就是post到了主线程的消息队列中等待处理。这里我们也就分析完毕。
不过这里有两个问题:
(1)前面我们知道由mAttachInfo的值来决定调用哪个post。那么mAttachInfo什么时候不为空?查看mAttachInfo被赋值的地方有两处:

void dispatchAttachedToWindow(AttachInfo info, int visibility) {
...
    mAttachInfo = info;
...
}
void dispatchDetachedFromWindow() {
...
        mAttachInfo = null;
...
}

从这里我们可以看到在dispatchAttachedToWindowmAttachInfo被赋值为info,这里的info不就是从performTraversals方法中调用的时候传入的吗~所以就是当attachedToWindow的时候被赋值,detachedFromWindow时被置为空。当mAttachInfo为空的时候将Runnable存放到HandlerAction中,当ViewdispatchAttachedToWindow方法被调用时使用主线程handler将其post到消息队列中。当mAttachInfo不为空的时候直接调用主线程handler即可。
(2)在performTraversals方法中我们是先调用dispatchAttachedToWindow方法之后才开始调用measurelayout等方法进行测量布局的,而在dispatchAttachedToWindow中我们就有调用了handler将消息post到消息队列了准备执行了,那此时我们为什么能够在View.post方法中获取到正确的长宽呢?
我们来查看performTraversals的调用时机:

 void doTraversal() {
          ...
          performTraversals();
          ...
    }

就一处被调用,继续看doTraversal被调用的地方:

final class TraversalRunnable implements Runnable {
        @Override
        public void run() {
            doTraversal();
        }
    }

我们可以看到其实它是在一个Runnable中的run方法中被调用,这个TraversalRunnable方法被实例化之后,

final TraversalRunnable mTraversalRunnable = new TraversalRunnable();

分别在scheduleTraversalsunscheduleTraversals方法中被调用,这两个方法直观上看上去就是成对出现的方法,我们来看scheduleTraversals方法中有一行代码:

 void scheduleTraversals() {
           ...
            mChoreographer.postCallback(
                    Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
            ...
        }
    }

这个postCallback方法貌似也是post的近亲?我们查看Choreographer中的这个方法可以看到其实最终就是调用了Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);方法。这个mHandler就是主线程Handler~
因此谜底揭开,首先在主线程的Handler中就已经post进去了一个Runnable来执行performTraversals方法,当然就按照顺序执行了post我们调用View.post(runnable)方法到主线程Handler、测量、布局、绘制等一系列操作。然而由于Handler的机制,它是将所有的message都post到一个MessageQueue中,按照顺序执行这些消息。因此只有当执行完测量、布局、绘制之后,才能执行我们的Runnable,所以我们这时就能够获取到正确的宽高了~

三.感想

第一次分析源码确实感觉很多知识点都不太理解,不过希望自己能坚持下来不断前行~分析的过程中有借鉴两位大神的文章,很感谢_
1.【Andorid源码解析】View.post() 到底干了啥
2.通过View.post()获取View的宽高引发的两个问题

你可能感兴趣的:(关于View.post()/postDelay()方法的一些分析)