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

目录

前言

1、postInvalidate

2、post

3、总结


前言

我们知道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<OnAttachStateChangeListener> 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<HandlerAction> mActions = new ArrayList<HandlerAction>();


    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<HandlerAction> 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还是有区别的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BennuCTech

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

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

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

打赏作者

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

抵扣说明:

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

余额充值