ActivityThread.java
public static void main(String[]args) {
//main函数进行消息的轮询。通过zygote进程,创建应用进程,执行main方法,这也是我们平时所说的UI主进程
Looper.prepareMainLooper();
ActivityThread thread = newActivityThread();
thread.attach(false);
Looper.loop();
}
private void attach(booleansystem) {
final IActivityManager mgr =ActivityManagerNative.getDefault();//与AMS关联
try{
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {
}
}
handleMessage
case LAUNCH_ACTIVITY:
case RESUME_ACTIVITY:
handleLaunchActivity
Activity a = performLaunchActivity(r,customIntent);
Activityactivity = null;
try{
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
//通过反射机制加载Activity
activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
}catch (Exception e) {}
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
if (activity != null) {
Context appContext = createBaseContextForActivity(r, activity);
activity.attach(appContext, this, getInstrumentation(),r.token,
r.ident, app, r.intent,r.activityInfo, title, r.parent,
r.embeddedID,r.lastNonConfigurationInstances, config,
r.referrer,r.voiceInteractor);
}
handleResumeActivity
View decor = r.window.getDecorView();
ViewManager wm = a.getWindowManager();
wm.addView(decor,l);
Activity.java
public void setContentView(int layoutResID) {
//getWindow()得到一个Window对象 mWindow,Window是一个抽象类。
Window是一个抽象类,mWindow对象的实现类就是PhoneWindow类
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
public Window getWindow() {
return mWindow;
}
final void attach(Context context,ActivityThread aThread,
……….
Configuration config, Stringreferrer, IVoiceInteractor voiceInteractor) {
mWindow= PolicyManager.makeNewWindow(this);
mWindow.setWindowManager(//关联WindowManager
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken,mComponent.flattenToString(),
(info.flags &ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
mWindowManager =mWindow.getWindowManager();
}
Policy.java
public Window makeNewWindow(Contextcontext) {
return new PhoneWindow(context);
}
PhoneWindow.java
public void setContentView(intlayoutResID) {
if (mContentParent == null) {
installDecor();
}
mContentParent.addView(view,params);
}
private final class DecorView extends FrameLayoutimplements RootViewSurfaceTaker {
}
Window.java
public void setWindowManager(WindowManagerwm, IBinder appToken, String appName, boolean hardwareAccelerated) {
mWindowManager= ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
WindowManagerImpl.java
private final WindowManagerGlobalmGlobal = WindowManagerGlobal.getInstance();
WindowManagerGlobal.java
public void addView(Viewview, ViewGroup.LayoutParams params, Display display, Window parentWindow) {
ViewRootImpl root;
root = new ViewRootImpl(view.getContext(),display);
}
ViewRootImpl.java
public void setView(Viewview, WindowManager.LayoutParams attrs, View panelParentView) {
requestLayout();
scheduleTraversals();
mHandler.post(mTraversalRunnable);
doTraversal();
performTraversals();
// Android系统View的绘制流程:依次执行View类里面的如下三个方法
measure(int ,int) :测量View的大小
layout(int ,int ,int ,int) :设置子View的位置
draw(Canvas) :绘制View内容到Canvas画布上
performMeasure(childWidthMeasureSpec,childHeightMeasureSpec);
performLayout(lp,desiredWindowWidth, desiredWindowHeight);
performDraw();
draw(fullRedrawNeeded);
view.assignParent(this);
}
public ViewParent invalidateChildInParent(int[]location, Rect dirty) {
}