【Launcher开发】Android桌面布局分析

    安卓桌面主要由Launcher和SystemUI组成。SystemUI负责可下拉状态栏、导航栏、锁屏页面的显示;Launcher则作为桌面的主要部分,负责各应用与Widget的排列与增删,其布局可占到整个桌面的90%以上。所以我们有必要先从布局层面开始了解其构成。

    从AndroidManifest.xml文件可以知道,Launcher.java是应用的主Activity,其布局也正是我们所看到的桌面,以下就是launcher.xml的主要内容。



    
    
        
        
        
        

        
        
            
            
            
            
            
            
        
        
        
        
        

        
        
            
        

        
        
        
    

    布局文件可能不够直观,这些控件在手机上的位置可用下面这幅图表示:                                                              【Launcher开发】Android桌面布局分析_第1张图片

    当HotSeat被创建时,默认会在中间添加一个all apps按钮,通过此按钮我们可以进入到all apps页面,当然,all apps页面也属于上面launcher.xml的一部分,只不过它在onCreate之后默认处于隐藏状态。以下代码就是HotSeat添加此按钮的关键处。

void resetLayout() {
        mContent.removeAllViewsInLayout();

        // Add the Apps button
        Context context = getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        BubbleTextView allAppsButton = (BubbleTextView)
                inflater.inflate(R.layout.application, mContent, false);
        allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
                context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
        allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
        allAppsButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mLauncher != null &&
                    (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
                    mLauncher.onTouchDownAllAppsButton(v);
                }
                return false;
            }
        });

        allAppsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                if (mLauncher != null) {
                    mLauncher.onClickAllAppsButton(v);
                }
            }
        });

        // Note: We do this to ensure that the hotseat is always laid out in the orientation of
        // the hotseat in order regardless of which orientation they were added
        int x = getCellXFromOrder(mAllAppsButtonRank);
        int y = getCellYFromOrder(mAllAppsButtonRank);
        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
        lp.canReorder = false;
        //添加all app到CellLayout的第x,y位置,即中间位置
        mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
    }
    可以看出此按钮本身也是一个BubbleTextView,不同的是它的点击事件没有被Launcher.java中的onClick接收,而是进行单独处理在onClickAllAppsButton方法中。而在onClickAllAppsButton方法就是展开launcher的apps_customize_pane布局进行显示。要展示的布局如下。


    
        
        
            
            

            
            
        
        
            

            
            


            
            
        
    
    
    
    

    我们可以用下面两幅图直观的表示,图一即是当第一次进入all apps页面时出现的提示遮罩。当我们长按图中所标识的地方时,会发现当前页面处于拖拽状态(此拖拽和DragLayer有关)。图二上部分是FocusOnlyTabWidget即Tab标签。右边的MarketButton由于系统中没有满足条件的应用所以没有显示出来。此页面排列了系统中所有的用户应用,当我们进行滑动时,底部还会出现指示器。

【Launcher开发】Android桌面布局分析_第2张图片 【Launcher开发】Android桌面布局分析_第3张图片

    OK,布局的话先介绍到这里,之后的文章我想从以下几个方面来理解Launcher。

        1.Launcher是启动流程分析。

        2.DragController如何控制拖动。

        3.Launcher数据库构建与CRUD的方面。

        4.FolderIcon的了解与深入。

        5.壁纸是显示到Launcher桌面的。

        6.WorSpace与CellLayout的代码结构。

        7.Launcher为适配TV做了哪些工作。

你可能感兴趣的:(安卓,Launcher源码阅读)