Android启动优化、布局优化必经之路—如何精准获取页面绘制时间

本文探讨了Android中如何通过view.post()方法精准获取页面绘制时间,解析了view.post()的源码,解释了即使在onResume()中使用也能在绘制完成后再执行的原因。内容涉及Android消息机制、绘制流程和性能优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看一下 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 是否为空,不为空的话,会直接调用 handler.post() 方法。也就是说,如果 attachInfo 对象不为空,view.post() 和 new Handler().post() 的效果是相同的。

反之,如果 attachInfo 为空,就会调用 mRunQueue 对象的 post() 方法

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

synchronized (this) {
if (mActions == null) {
mActions = new HandlerAction[4];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值