本項(xiàng)目以Window為基礎(chǔ),以AlertDialog源碼和PhoneWindow為原型,用簡化的方式
完成AlertDialog的各項(xiàng)功能,源碼已上傳到github:https://github.com/shuhuaxie/MockDialogAndPhoneWindow.
腦圖網(wǎng)址:http://naotu.baidu.com/file/ef8c63e1523ba6fd81af3eef2fc7497a?token=92a6474c6631b4ee (建議在電腦上閱讀)
功能概要和實(shí)現(xiàn)流程
-
顯示
WindowManager.addView添加需要展示的界面
Dialog在show時,才將生成Layout添加到DecorView中,然后打包到WindowManager中
-
返回鍵
DecorView.dispatchKeyEvent事件分發(fā)
WindowManager.removeViewImmediate執(zhí)行實(shí)際的關(guān)閉操作
-
修改UI
PhoneWindow.generateLayout設(shè)置添加到WindowManager的參數(shù),使界面居中背景半透明
-
點(diǎn)擊邊緣關(guān)閉
DecorView.dispatchTouchEvent
-
其他
事件分發(fā)的基本流程是
WindowManager->DecorView->Activity/Dialog->ContentView