非UI线程下页面处理:view的postInvalidate和post对消息处理的差异化

Android进阶之路系列:http://blog.csdn.net/column/details/16488.html

我们知道view有一系列post方法,用于在非UI线程中发出一些页面处理。view还有另外一个postInvalidate方法,同样在非UI线程中发起重绘。
同样是在非UI线程向UI线程发出消息,但是这里面有很大的区别。

1、postInvalidate

先来看看postInvalidate

public void postInvalidate() {
    postInvalidateDelayed(0);
}

public void postInvalidateDelayed(long delayMilliseconds) {
    // We try only with the AttachInfo because there's no point in invalidating
    // if we are not attached to our window
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
    }
}

可以看到当mAttachInfo为null的时候,这个流程就直接结束了。而mAttachInfo则是当view被DetachedFromWindow的时候会被置为null,代码如下:

void dispatchDetachedFromWindow() {
    AttachInfo info = mAttachInfo;
    if (info != null) {
        int vis = info.mWindowVisibility;
        if (vis != GONE) {
            onWindowVisibilityChanged(GONE);
        }
    }

    onDetachedFromWindow();
    onDetachedFromWindowInternal();

    InputMethodManager imm = InputMethodManager.peekInstance();
    if (imm != null) {
        imm.onViewDetachedFromWindow(this);
    }

    ListenerInfo li = mListenerInfo;
    final CopyOnWriteArrayList listeners =
            li != null ? li.mOnAttachStateChangeListeners : null;
    if (listeners != null && listeners.size() > 0) {
        for (OnAttachStateChangeListener listener : listeners) {
            listener.onViewDetachedFromWindow(this);
        }
    }

    if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
        mAttachInfo.mScrollContainers.remove(this);
        mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
    }

    mAttachInfo = null;
    if (mOverlay != null) {
        mOverlay.getOverlayView().dispatchDetachedFromWindow();
    }
}

所以当view被从页面上移除后,postInvalidate就无效了。
当mAttachInfo不为null的时候,则执行mViewRootImpl的dispatchInvalidateDelayed函数,代码如下:

public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
    Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
    mHandler.sendMessageDelayed(msg, delayMilliseconds);
}

直接用mHandler发出了消息。

2、post

下面再来看看post

public boolean post(Runnable action) {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler.post(action);
    }
    // Assume that post will succeed later
    ViewRootImpl.getRunQueue().post(action);
    return true;
}

同样当mAttachInfo不为null的时候,直接使用mHandler发出消息。
但是!注意但是!当mAttachInfo为null时,并不直接结束流程,而是将runnable存入了一个RunQueue。RunQueue是一个队列,部分代码如下:

static final class RunQueue {
    private final ArrayList mActions = new ArrayList();

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

    void postDelayed(Runnable action, long delayMillis) {
        HandlerAction handlerAction = new HandlerAction();
        handlerAction.action = action;
        handlerAction.delay = delayMillis;

        synchronized (mActions) {
            mActions.add(handlerAction);
        }
    }

    void removeCallbacks(Runnable action) {
        ...
    }

    void executeActions(Handler handler) {
        synchronized (mActions) {
            final ArrayList actions = mActions;
            final int count = actions.size();

            for (int i = 0; i < count; i++) {
                final HandlerAction handlerAction = actions.get(i);
                handler.postDelayed(handlerAction.action, handlerAction.delay);
            }

            actions.clear();
        }
    }

    private static class HandlerAction {
        ...
    }
}

当RunQueue的executeActions函数被调用时,会遍历队列再去用handler发送消息。
那么executeActions什么时候被调用?
在ViewRootImpl的performTraversals函数中,如下:

private void performTraversals() {
        ...
        // Execute enqueued actions on every traversal in case a detached view enqueued an action
        getRunQueue().executeActions(mAttachInfo.mHandler);
        ...
}

而performTraversals在doTraversal函数中被调用

void doTraversal() {
    if (mTraversalScheduled) {
        mTraversalScheduled = false;
        mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);

        if (mProfile) {
            Debug.startMethodTracing("ViewAncestor");
        }

        performTraversals();

        if (mProfile) {
            Debug.stopMethodTracing();
            mProfile = false;
        }
    }
}

doTraversal则在ViewRootImpl中一个Runnable对象mTraversalRunnable中执行

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

mTraversalRunnable则在ViewRootImpl的scheduleTraversals函数中被post出去

void scheduleTraversals() {
    if (!mTraversalScheduled) {
        mTraversalScheduled = true;
        mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
        mChoreographer.postCallback(
                Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
        if (!mUnbufferedInputDispatch) {
            scheduleConsumeBatchedInput();
        }
        notifyRendererOfFramePending();
        pokeDrawLockIfNeeded();
    }
}

而scheduleTraversals则在很多地方被执行,比如:

void handleAppVisibility(boolean visible) {
    if (mAppVisible != visible) {
        mAppVisible = visible;
        scheduleTraversals();
        if (!mAppVisible) {
            WindowManagerGlobal.trimForeground();
        }
    }
}

void handleGetNewSurface() {
    mNewSurfaceNeeded = true;
    mFullRedrawNeeded = true;
    scheduleTraversals();
}

...

这里就不一一列举了,大家有兴趣可以自己去源码里搜索一下。
总结一下就是当view被从页面上移除后,通过post系列函数传的消息并不会立刻用handler发出去,而是先将其存入一个队列里。当view再次被添加到页面上时,会从队列中的取出消息再用handler发出去。

3、总结

所以当我们使用

post(new Runnable() {
    @Override
    public void run() {
        invalidate();
    }
});

它其实与postInvalidate还是有区别的。

Android进阶之路系列:http://blog.csdn.net/column/details/16488.html

你可能感兴趣的:(非UI线程下页面处理:view的postInvalidate和post对消息处理的差异化)