Android源码解析Window系列第(二)篇---Dialog加载绘制流程
来源:互联网 发布:城市poi数据 编辑:程序博客网 时间:2024/06/05 16:32
上一篇分析了一下Activity的Window创建过程和Window与Activity是如何关联到一起的,通过上一篇,我们对Window有了基本的认识。这一篇分享一下我对Dialog加载绘制流程的理解。
首先创建一个Dialog,回顾下创建Dialog的流程。
public class MainActivity extends Activity { AlertDialog alertDialog=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); builder.setMessage("Message部分"); builder.setTitle("Title部分"); builder.setView(R.layout.activity_main); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); } }); alertDialog = builder.create(); alertDialog.show(); }}
运行效果如下,截图中hello world是main布局里面的。
我们看一下Dialog的部分代码
public class AlertDialog extends AppCompatDialog implements DialogInterface { final AlertController mAlert; static final int LAYOUT_HINT_NONE = 0; static final int LAYOUT_HINT_SIDE = 1; protected AlertDialog(@NonNull Context context) { this(context, 0); } /** *构造函数,可以指定主题,比如 R.attr#alertDialogTheme */ protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) { super(context, resolveDialogTheme(context, themeResId)); mAlert = new AlertController(getContext(), this, getWindow()); } /** *构造函数,点击外部是否可取消,取消时候的回调 */ protected AlertDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) { this(context, 0); setCancelable(cancelable); setOnCancelListener(cancelListener); } /** 获取对话框中的按钮,比如可以获取BUTTON_POSITIVE */ public Button getButton(int whichButton) { return mAlert.getButton(whichButton); } //设置标题,内部调用了 mAlert.setTitle(title) @Override public void setTitle(CharSequence title) { super.setTitle(title); mAlert.setTitle(title); } /** * 标题也可能是一个View,此时设置标题用这个方法 */ public void setCustomTitle(View customTitleView) { mAlert.setCustomTitle(customTitleView); } public void setMessage(CharSequence message) { mAlert.setMessage(message); } ....... public static class Builder { private final AlertController.AlertParams P; private final int mTheme; //构造函数 public Builder(@NonNull Context context) { this(context, resolveDialogTheme(context, 0)); } //构造函数 public Builder(@NonNull Context context, @StyleRes int themeResId) { P = new AlertController.AlertParams(new ContextThemeWrapper( context, resolveDialogTheme(context, themeResId))); mTheme = themeResId; } @NonNull public Context getContext() { return P.mContext; } public Builder setTitle(@StringRes int titleId) { P.mTitle = P.mContext.getText(titleId); return this; } public Builder setTitle(CharSequence title) { P.mTitle = title; return this; } public Builder setCustomTitle(View customTitleView) { P.mCustomTitleView = customTitleView; return this; } public Builder setMessage(@StringRes int messageId) { P.mMessage = P.mContext.getText(messageId); return this; } public Builder setMessage(CharSequence message) { P.mMessage = message; return this; } ....... public AlertDialog create() { final AlertDialog dialog = new AlertDialog(P.mContext, mTheme); //Dialog的参数其实保存在P这个类里面 P.apply(dialog.mAlert); dialog.setCancelable(P.mCancelable); if (P.mCancelable) { dialog.setCanceledOnTouchOutside(true); } dialog.setOnCancelListener(P.mOnCancelListener); dialog.setOnDismissListener(P.mOnDismissListener); if (P.mOnKeyListener != null) { dialog.setOnKeyListener(P.mOnKeyListener); } return dialog; } public AlertDialog show() { final AlertDialog dialog = create(); dialog.show(); return dialog; } }}
从代码中可以看到,我们创建的Dialog是什么样式,可以通过Builder的set系列方法指定,是当我们调用Builder的set系列方法的时候,会将我们传递的参数保存在P中,这个P是什么呢?在Builder有一个成员
private final AlertController.AlertParams P;
P是AlertController的内部类AlertParams类型的变量。AlertParams中包含了与AlertDialog视图中对应的成员变量。调用Builder的set系列方法之后,我们传递的参数就保存在P中了。P存在之后,我们就可以创建对话框了。这个就好像,我们要建造房子,得先有设计图纸,图纸决定了房子的样子。
public AlertDialog create() { final AlertDialog dialog = new AlertDialog(P.mContext, mTheme); P.apply(dialog.mAlert);}
用new的方式创建一个AlertDialog,AlertDialog的构造函数中调用了super,AlertDialog继承Dialog,看一下Dialog的构造函数。
Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) { .... mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); final Window w = new PhoneWindow(mContext); mWindow = w; w.setCallback(this); w.setOnWindowDismissedCallback(this); w.setWindowManager(mWindowManager, null, null); w.setGravity(Gravity.CENTER); mListenersHandler = new ListenersHandler(this); }
可以发现直接new了一个PhoneWindow赋值给mWindow,并且设置了callback回调,当窗口状态发生变化的时候,就会回调Dialog中的callback实现。这个和Activity的一模一样,之前已经分析过了。当你new AlertDialog()之后,到此,Dialog所需要的Window就有了。接下来,就要分析,Dialogd的视图怎么与Window做关联了,继续往下面走。
在AlertDialog创建之后,就调用 P.apply(dialog.mAlert),将P中的参数赋值给dialog的mAlert,mAlert是AlertController类型。我们看一下apply函数。
public void apply(AlertController dialog) { if (mCustomTitleView != null) { dialog.setCustomTitle(mCustomTitleView); } else { if (mTitle != null) { dialog.setTitle(mTitle); } if (mIcon != null) { dialog.setIcon(mIcon); } if (mIconId != 0) { dialog.setIcon(mIconId); } if (mIconAttrId != 0) { dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId)); } } if (mMessage != null) { dialog.setMessage(mMessage); } if (mPositiveButtonText != null) { dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText, mPositiveButtonListener, null); } if (mNegativeButtonText != null) { dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText, mNegativeButtonListener, null); } if (mNeutralButtonText != null) { dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText, mNeutralButtonListener, null); } if ((mItems != null) || (mCursor != null) || (mAdapter != null)) { createListView(dialog); } if (mView != null) { if (mViewSpacingSpecified) { dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight, mViewSpacingBottom); } else { dialog.setView(mView); } } else if (mViewLayoutResId != 0) { dialog.setView(mViewLayoutResId); } }
代码很简单,就纯粹做一件事,把P中的参数赋值给dialog的mAlert中对应的参数,比如标题,按钮等等。这些都没有什么,下面干货来了。继续看Dialog的show方法,记住,我们现在的任务是,分析Dialog视图是怎么与Window做关联的!。
public void show() { if (mShowing) { if (mDecor != null) { if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR); } mDecor.setVisibility(View.VISIBLE); } return; } mCanceled = false; if (!mCreated) { dispatchOnCreate(null); } onStart(); mDecor = mWindow.getDecorView(); if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { final ApplicationInfo info = mContext.getApplicationInfo(); mWindow.setDefaultIcon(info.icon); mWindow.setDefaultLogo(info.logo); mActionBar = new WindowDecorActionBar(this); } WindowManager.LayoutParams l = mWindow.getAttributes(); if ((l.softInputMode & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) { WindowManager.LayoutParams nl = new WindowManager.LayoutParams(); nl.copyFrom(l); nl.softInputMode |= WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; l = nl; } mWindowManager.addView(mDecor, l); mShowing = true; sendShowMessage(); }
看下面,当mCreated为假的时候,调用dispatchOnCreate,最终是回调了Dialog的onCreate方法。
if (!mCreated) { dispatchOnCreate(null); }
void dispatchOnCreate(Bundle savedInstanceState) { if (!mCreated) { onCreate(savedInstanceState); mCreated = true; } }protected void onCreate(Bundle savedInstanceState) {}
Dialog里面的onCreate是个空实现,我们要看看AlertDialog的onCreate方法的里面的东西。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAlert.installContent(); }
调用 mAlert.installContent()方法,上面说了mAlert是AlertController类型,AlertController中有Dialog所需要的样式参数。
public void installContent() { final int contentView = selectContentView(); mDialog.setContentView(contentView); setupView(); }
有木有!!!终于发现了setContentView方法,先通过selectContentView布局文件的ID,然后调用Window的setContentView方法,加载指定的文件到DecorView的Content部分,并且回调onContentChanged方法通知Dialog。
@Override public void setContentView(int resId) { ensureSubDecor(); ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content); contentParent.removeAllViews(); LayoutInflater.from(mContext).inflate(resId, contentParent); mOriginalWindowCallback.onContentChanged(); }
setContentView执行之后,调用了setupView方法和setupDector方法,这两个方法的主要作用就是初始化布局文件中的组件和Window对象中的mDector成员变量。现在回到我们的show方法,在执行了dispatchOnCreate方法之后我们又调用了onStart方法。在看show方法里面,会调用下面三行代码。
mDecor = mWindow.getDecorView(); WindowManager.LayoutParams l = mWindow.getAttributes(); mWindowManager.addView(mDecor, l);
最终会通过WindowManager将DecorView添加到Window之中,OK到这里整个Dialog的界面就会被绘制出来了。
总结一下:AlertDialog和Activity一样,内部有一个Window,我们构造AlertDialog.Builder,通过Builder设置Dialog各种属性,,这些参数会被放在一个名为P(AlertController类型)的变量中,在调用AlertDialog.Builder.create方法的时候,内部首先会new一个 AlertDialog,AlertDialog的父类Dialog的构造函数中会new一个PhoneWindow赋值给AlertDialog中的Window,并且为它设置了回调。AlertDialog创建之后执行apply方法,将P中的参数设置赋值给Dialog,后我们调用Dialog.show方法展示窗口,内部调用dispatchOnCreate,最终会走到setContentView,到此Dialog的Window和Dialog视图关联到了一起,最后执行mWindowManager.addView方法,通过WindowManager将DecorView添加到Window之中,此时Dialog显示在了我们面前。
Please accept mybest wishes for your happiness and success!
- Android源码解析Window系列第(二)篇---Dialog加载绘制流程
- Android源码解析(十九)-->Dialog加载绘制流程
- Android源码解析(二十)-->Dialog取消绘制流程
- Android源码基础解析之Dialog加载绘制流程
- Android源码解析(二十一)-->PopupWindow加载绘制流程
- Android源码解析(二十二)-->Toast加载绘制流程
- Android源码基础解析之Dialog取消绘制流程
- Android源码解析Window系列第(一)篇---Window的基本认识和Activity的Window创建过程
- Android源码解析Window系列第(三)篇---WindowManager基本原理
- Android源码解析Handler系列第(二)篇--- ThreadLocal详解
- Android源码解析Handler系列第(二)篇--- ThreadLocal详解
- android应用程序窗口框架学习(2)-view绘制流程源代码解析-setContentView与LayoutInflater加载解析机制源码分析
- Android源码解析(十八)-->Activity布局绘制流程
- Android View绘制流程与源码解析
- Android视图绘制流程完全解析(二)
- Android-PickerView系列之源码解析篇(二)
- Android-PickerView系列之源码解析篇(二)
- Dialog创建流程源码解析
- 在Archlinux下用IDEA发布JavaWeb项目
- replace和replaceAll的一点发现
- openstack网络(三)
- JPA映射关联关系-双向一对一
- net.ipv4.tcp_tw_recycle
- Android源码解析Window系列第(二)篇---Dialog加载绘制流程
- Android---webview实现网页的点击跳转
- 抽奖之拉抽屉
- 魔方阵的应用
- node.js学习日记(1)
- LDD之IO端口与IO内存
- 在二叉查找树中插入节点
- vtk 体绘制
- (转)高并发高流量网站架构详解