1.Android UI线ç¨
Android UI线ç¨
æèï¼å å¿ é¡»äºè§£ä¸é¢2个é®é¢
1.顾åæä¹ UIçº¿ç¨ å°±æ¯å·æ°UI æå¨çº¿ç¨
2.UIæ¯å线ç¨å·æ°
1.对Activity æ¥è¯´ UI线ç¨å°±æ¯å ¶ä¸»çº¿ç¨
2.对Viewæ¥è¯´ UI线ç¨å°±æ¯å建ViewRootImplæå¨ç线ç¨
å¯ä»¥éè¿ WindowManager å é¨ä¼å建ViewRootImpl对象
好äºï¼è¿å ¥ä¸»é¢ãæ们æ¥æ ¢æ ¢æå¼é¢çº±ã
æ们å¯ä»¥åå«ä»å 个æ¹é¢åå ¥
æ们å¯è½é½æ使ç¨è¿ runOnUiThread ç°å¨æ¥çççæºç å®ç°ã
å¯ä»¥ä»ä¸é¢çæºç çå°
ä¸æ¯UIçº¿ç¨ å°±ç¨Handleråå°Handleræå¨ç线ç¨ä¸ï¼å¦ææ¯UI线ç¨ç´æ¥å°±è°ç¨runæ¹æ³ã
Activityçå建ï¼
1.Activityå建ï¼mInstrumentation.newActivity
2.å建Context ï¼ContextImpl appContextcreateBaseContextForActivity(r)
æ们ç»å¸¸ç¨è¿ä¸ªæ¹æ³å¹²çäºæ å°±æ¯ï¼è¦ä¹å¨onCreateä¸è·åView宽é«çå¼ãè¦ä¹å°±æ¯å¨å线ç¨ä¸åä¸äºèæ¶æä½ ï¼ç¶åpoståå°å¯¹åºViewæå¨ççº¿ç¨ æ¥ç»å¶UIæä½ãé£ä¹è¿ä¸ªå¯¹åºç线ç¨å°±æ¯UI线ç¨äºã
é£ä¹è¿ä¸ªUI线ç¨å°±ä¸å®æ¯ä¸»çº¿ç¨åï¼
æ¥æ¥ç»§ç»æ¥çãå®çæºç View:post
mAttachInfo å¨dispatchAttachedToWindow ä¸è¢«èµå¼ ï¼ä¹å°±æ¯å¨ViewRootImplå建çæ¶åï¼æ以æ¯å建ViewRootImplæå¨ç线ç¨ã
attachInfo ä¸é¢æ¶å为null å¢ï¼å¨ViewRootImpl è¿æ²¡æ¥å¾åå建çæ¶åï¼ViewRootImpl å建æ¯å¨ âonResume" ä¹åãæä»¥å¨ Activity ç onCreate å»View.post é£ä¹AttachInfo æ¯ä¸ºnull ã
å½ AttachInfo == null é£ä¹ä¼è°ç¨ getRunQueue().post(action) ã
æç»è¿ä¸ªRunnable 被 ç¼åå° HandlerActionQueue ä¸ã
ç´å°ViewRootImpl ç performTraversals ä¸ è°ç¨dispatchAttachedToWindow(mAttachInfo,建站优化源码模板 0);ï¼ é£ä¹æä¼å»å¤ç RunQueue() ä¸çRunnableã
æ¥å¼ å¾ ä¾¿äºç解è¿ä¸ªæµç¨
æ们ææ¶åå»å线ç¨æä½UIçæ¶å(å¦ï¼requestLayout)ï¼ä¼å¾ç»å¸¸è§å°ä¸é¢ç æ¥éæ¥å¿ï¼
Only the original thread that created a view hierarchy can touch its views
为ä»ä¹ä¼æ¥è¿ä¸ªé误å¢ï¼
ç¿»è¯ä¸ä¸ï¼åªæå建è§å¾å±æ¬¡ç»æçåå§çº¿ç¨æè½æ¥è§¦å°å®çè§å¾ã
ä¹å°±æ¯æä½UIç线ç¨è¦åViewRootImplå建ç线ç¨æ¯åä¸ä¸ªçº¿ç¨æè¡ï¼å¹¶ä¸æ¯åªæ主线ç¨æè½æ´æ°UIåã
ViewRootImplå建ç线ç¨ï¼é£ä¹ ViewRootImpl å¨åªé被å建çå¢ï¼
ä»ä¸å¾å¯ä»¥çå°ViewRootImplå建æå¼å§æ¯ä» ActivityThread çHandleResumeActivityä¸å¼å§ ä¸ç´ ViewRootImpl å建ï¼ä¹å°±æ¯è¯´ViewRootImpl 对åºçUI线ç¨å ActivityThread å¨åä¸ä¸ªçº¿ç¨ ä¹å°±æ¯ä¸»çº¿ç¨ã
å¥½äº éè¿ä¸é¢ç讲解ï¼ä¸é¢çé®é¢ç¸ä¿¡ä½ å¯ä»¥èªå·±åçå¦~