Anroid基础建设之View,Window,Activity

本文深入探讨了Android系统中PhoneWindow类及其与WindowManager的关系,详细解释了DecorView的创建过程及如何成为WindowManager中的视图。

 

1.PhoneWindow

DecorView存在于PhoneWindow类中, 这两个成员变量是属于包含关系,mDecor包含mContentParent

     private DecorView mDecor;
     private ViewGroup mContentParent;

setContentView 调用 installDecor ,installDecor调用generateDecor 后调用 generateLayout,来产生上面两个变量

   mDecor和Layout,Layout里面包含了mContentParent

  下面的代码证明了系统的默认window的资源文件的名称和位置,可以去framework找到。

        // Inflate the window decor.

        int layoutResource;
        int features = getLocalFeatures();
        // System.out.println("Features: 0x" + Integer.toHexString(features));
        if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
            if (mIsFloating) {
                layoutResource = com.android.internal.R.layout.dialog_title_icons;
            } else {
                layoutResource = com.android.internal.R.layout.screen_title_icons;
            }
            // System.out.println("Title Icons!");
        } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0) {
            // Special case for a window with only a progress bar (and title).
            // XXX Need to have a no-title version of embedded windows.
            layoutResource = com.android.internal.R.layout.screen_progress;
            // System.out.println("Progress!");
        } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
            // Special case for a window with a custom title.
            // If the window is floating, we need a dialog layout
            if (mIsFloating) {
                layoutResource = com.android.internal.R.layout.dialog_custom_title;
            } else {
                layoutResource = com.android.internal.R.layout.screen_custom_title;
            }
        } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
            // If no other features and not embedded, only need a title.
            // If the window is floating, we need a dialog layout
            if (mIsFloating) {
                layoutResource = com.android.internal.R.layout.dialog_title;
            } else {
                layoutResource = com.android.internal.R.layout.screen_title;
            }
            // System.out.println("Title!");
        } else {
            // Embedded, so no decoration is needed.
            layoutResource = com.android.internal.R.layout.screen_simple;
            // System.out.println("Simple!");
        }

 

 

 

 mDecor除了mContentParent之外,还有以下的View

 

    private TextView mTitleView;
      private ImageView mLeftIconView;
    private ImageView mRightIconView;
    private ProgressBar mCircularProgressBar;
    private ProgressBar mHorizontalProgressBar;

除了VIew之外,还有一个背景

    private Drawable mBackgroundDrawable;

另外还有这几个特殊的东西

  private DrawableFeatureState[] mDrawables;
    private PanelFeatureState[] mPanels;
    private PanelFeatureState mPreparedPanel;

这几个对应的是各种对话框

    public static final int FEATURE_OPTIONS_PANEL = 0;

    public static final int FEATURE_CONTEXT_MENU = 6;

 

PhoneWindow用自己的话说,到底是什么?

这些所有的东西构成了一个Window。

    现在可以定义一个Window了,Window就是一些View的集合。有tile,有Dialog有contentview,还有两个icon,还有背景,还有两种ProgressBar

 

 

下面的代码在ActivityThread的handleResumeActivity方法中

 r.window = r.activity.getWindow();
                View decor = r.window.getDecorView();
                decor.setVisibility(View.INVISIBLE);
                ViewManager wm = a.getWindowManager();
                WindowManager.LayoutParams l = r.window.getAttributes();
                a.mDecor = decor;
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
                l.softInputMode |= forwardBit;
                if (a.mVisibleFromClient) {
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
                }

 PhoneWindow的decorView被安装到了WindowManager中。

 

  下面的代码说明了另外一个问题,就是PhoneWindow如何创建的问题Activity的attach方法中

  很搞笑的是Window并不是由WindowManager创建的,而是由PolicyManager创建的。

   

 mWindow = PolicyManager.makeNewWindow(this);

 

 下面的代码说明了另外一个,DecorView的创建问题:就是Activity的setContentView方法 ,  

 public void setContentView(int layoutResID) {
        getWindow().setContentView(layoutResID);
    }

 

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        mContentParent.addView(view, params);
        final Callback cb = getCallback();
        if (cb != null) {
            cb.onContentChanged();
        }
    }

 

2.WindowManager:

     继承于ViewManager,里面有三个重要的数组

   private View[] mViews;
    private ViewRoot[] mRoots;
    private WindowManager.LayoutParams[] mParams;

  根据数组下标进行映射,构成了一个 三项MAP。

   mRoots实际上是一个handler,这个handler通过callback给Activity发送键盘和鼠标事件。

 

WindowManagerImpl用自己的话说,到底是什么:

  WindowManagerImpl实际上并没有管理Window,管理的是Window与WindowManagerService的对话,mRoots正是这些对话的一个缩影。从继承关系来看,ViewRoot不是一个View,而是实现了ViewParent的Handler,包含一个Surface来绘图。

 

 

ViewRoot是一个真正的根。

 

1.创建一个Surface

2.提供两个对话IWindow和IWindowSession

3.提供Handler对事件进行处理。

 

 

 

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值