Activity页面的绘制流程,移动app开发公司

本文深入探讨了Android中Activity页面的绘制流程,重点关注ViewRootImpl的创建和Choreographer的作用。通过分析Choreographer的构造及工作原理,揭示了16.6ms屏幕刷新背后的Vsync信号和消息处理机制,以及如何调度绘制任务。对于移动开发者,理解这一流程对提升应用性能至关重要。
摘要由CSDN通过智能技术生成

ViewRootImpl root;

View panelParentView = null;

1.创建ViewRootImpl对象

root = new ViewRootImpl(view.getContext(), display);

view.setLayoutParams(wparams);

mViews.add(view);

mRoots.add(root);

mParams.add(wparams);

// do this last because it fires off messages to start doing things

try {

2.调用ViewRootImpl的setView方法

root.setView(view, wparams, panelParentView);

} catch (RuntimeException e) {

// BadTokenException or InvalidDisplayException, clean up.

if (index >= 0) {

removeViewLocked(index, true);

}

throw e;

}

}

}

注释1是个很重要的点,这里创建了一个ViewRootImpl对象。

我们来解析一下这个ViewRootImpl里面需要关注的几个点。

我们先看看ViewRootImpl的构方法:

public ViewRootImpl(Context context, Display display) {

//获取Session对象

mWindowSession = WindowManagerGlobal.getWindowSession();

mDisplay = display;

mBasePackageName = context.getBasePackageName();

//主线程

mThread = Thread.currentThread();

//创建Choreographer对象,这个对象很重要,可以把它理解为一个Handler

//Android 16.6ms刷新一次页面它启到了主要作用

//我们马上看看这个Choreographer是个什么

mChoreographer = Choreographer.getInstance();

mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);

}

ViewRootImpl的构造方法里面我们暂时值关注两件事

1.获取了Session对象这个对象是我们与WindowManagerService交互的桥梁。

2.创建了Choreographer对象。

我们现来看看这个Choreographer对象到底是什么,为什么它那么重要呢?

public static Choreographer getInstance() {

return sThreadInstance.get();

}

// Thread local storage for the choreographer.

private static final ThreadLocal sThreadInstance =

new ThreadLocal() {

@Override

protected Choreographer initialValue() {

//注意了这里其实就是主线程的Looper

//ViewRootImpl对象就是在主线程创建的

Looper looper = Looper.myLooper();

if (looper == null) {

throw new IllegalStateException(“The current thread must have a looper!”);

}

//创建Choreographer对象

Choreographer choreographer = new Choreographer(looper, VSYNC_SOURCE_APP);

if (looper == Looper.getMainLooper()) {

mMainInstance = choreographer;

}

return choreographer;

}

};

private Choreographer(Looper looper, int vsyncSource) {

mLooper = looper;

//前面我们说了正常情况瞎这个looper对象就是主线程的looper对象

//所以通过这个Handler发送的消息都是在主线程处理的

mHandl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值