Android Launcher分析和修改5——HotSeat分析

来源:互联网 发布:linux setid 编辑:程序博客网 时间:2024/05/16 17:41

转载地址:http://www.apkbus.com/android-125394-1-1.html

今天主要是分析一下Launcher里面的快捷方式导航条——HotSeat,一般我们使用手机底下都会有这个导航条,但是如果4.0的Launcher放到平板电脑里面运行,默认是没有HotSeat的,刚好我这里的运行环境类似平板,系统默认把HotSeat去掉了。办法,只能自己想办法把它弄出来,所以今天主要是分析如何在你Launcher上添加HotSeat以及分析HotSeat实现。

  Hotseat配置是通过配置文件控制的,一般来说,你需不需要Hotseat只要在配件文件里面写一下就OK,不过Hotseat有一个比较麻烦的地方,就是需要注意横屏还是竖屏。默认竖屏的时候,Hotseat是屏幕底下的,横屏的时候,在屏幕右边。不知道google当时为啥要这样设计,可能是为了横屏的时候,不占用本来就不多的竖向的空间吧。不过这个设计对于一些横屏的平板电脑或者移动设备,用户体验实在不太好。


1、Hotseat配置文件

  下面我们看看Hotseat的配置文件,Hotseat是属于workspace的,所以需要在workspace配置文件里面配置,打开launcher.xml就可以看到hotseat的配置,这个并不是所有launcher.xml文件都有hotseat属性。例如:layout-sw600dp文件夹下的launcher.xml就是默认没有hotseat配置,这个使用在大屏幕,平板之类的设置上。而我的设备刚好是使用这个配置。

所以把hotseat加到layout-sw600dp下的launcher.xml配置文件:

  1. <!-- WorkSpace最下面的五个快捷位置  mythou-->
  2.     <include layout="@layout/hotseat"
  3.         android:id="@+id/hotseat"
  4.         android:layout_width="match_parent"
  5.         android:layout_height="@dimen/button_bar_height_plus_padding"
  6.         android:layout_gravity="bottom" />
复制代码
 注意,我这里是使用了竖屏时的hotseat配置,因为我希望hotseat是放到屏幕下方。所以android:layout_gravity=
"bottom"也是配置为bottom。hotseat默认是有5个按钮,其中中间一个是进入AllApp列表的按钮,这个是程序里面设置
(下面会说到)。其他的默认按钮需要在default_workspace.xml里面配置。
  1. <!-- Hotseat (We use the screen as the position of the item in the hotseat) -->
  2.   <!-- 使用screen作为按钮位置标识-->
  3.     <favorite
  4.         launcher:packageName="com.example.naviback"
  5.         launcher:className="com.example.naviback.MainActivity"
  6.         launcher:container="-101"
  7.         launcher:screen="0"
  8.         launcher:x="0"
  9.         launcher:y="0" />
  10.     <favorite
  11.         launcher:packageName="com.csr.dvd"
  12.         launcher:className="com.csr.dvd.LoadDVD"
  13.         launcher:container="-101"
  14.         launcher:screen="1"
  15.         launcher:x="1"
  16.         launcher:y="0" />
  17.     <favorite
  18.         launcher:packageName="com.apical.apicalradio"
  19.         launcher:className="com.apical.apicalradio.RadioMainActivity"
  20.         launcher:container="-101"
  21.         launcher:screen="3"
  22.         launcher:x="3"
  23.         launcher:y="0" />
  24.     <favorite
  25.         launcher:packageName="com.csr.BTApp"
  26.         launcher:className="com.csr.BTApp.CSRBluetoothDemoActivity"
  27.         launcher:container="-101"
  28.         launcher:screen="4"
  29.         launcher:x="4"
  30.         launcher:y="0" />
复制代码
default_workspace的配置,我在第一篇文章里面已经说过了,不清楚的可以点击这里 。配置hotseat的属性跟workspace的有点不一样,下面针对不同的属性进行说明:
  • launcher:container:需要标识为-101 ,代表是hotseat的默认按钮。
  • launcher:screen:代表按钮的位置,0是第一个位置。ALlApp按钮默认是2,所以上面并没有screen为2的标签。
其他的属性跟workspace配置的属性一样,可以参考我写的第一篇文章。

配置完hotseat的默认按钮后,我们需要修改hotseat.xml的配置属性才能正常显示,下面是hotseat.xml的配置,我是使用了竖屏时的hotseat配置。
  1. <com.android.launcher2.Hotseat
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
  4.     android:background="@drawable/hotseat_bg_panel"  <!--设置hotseat的背景 --> 
  5.     launcher:cellCountX="5"   <!-- 代表hotseat横向有多少个方格(图标) -->
  6.     launcher:cellCountY="1">  <!-- 代码hotseat竖向有多少个方格-->
  7.     <com.android.launcher2.CellLayout     <!--实际容纳按钮的容器是CellLayout -->
  8.         android:id="@+id/layout"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:layout_gravity="center"
  12.      <!--下面几个属性就是控制5个按钮的显示位置调整,跟我们一般使用控件属性一样 -->
  13.         android:paddingTop="3dp"
  14.         android:paddingBottom="3dp"
  15.         android:paddingLeft="150dp"
  16.         android:paddingRight="@dimen/button_bar_height_bottom_padding"
  17.      <!-- hotseat里面按钮的大小以及两个按钮之间的间距 -->
  18.         launcher:cellWidth="106dip"
  19.         launcher:cellHeight="106dip"
  20.         launcher:widthGap="25dp"
  21.         launcher:heightGap="@dimen/hotseat_height_gap"
  22.         launcher:maxGap="@dimen/workspace_max_gap" />
  23. </com.android.launcher2.Hotseat>
复制代码
上面的属性,有几个我们是需要留意,因为这是直接关系我们hotseat的显示效果。上面我已经给出了一些关键属性大部分跟我们使用一般控件是一样的,其他的launcher:XXX就是launcher自己定义的属性。上面已经给出注释。需要注意的是launcher:cellCountX和launcher:cellCountY两个属性,这个跟横向竖向的hotseat有关。另外就是从中我们也可以看到其实hotseat可以定义多行多列。因为hotseat里面其实是包含了一个CellLayout,跟workspace一样。  除了设置Hotseat的属性外,我们还需要设置workspace的属性,以为hotseat占用了一部分的空间,所以workspace就需要腾出一部分空间处理。例如原来你的workspace没有加入hotseat前是5*3设置,如果需要加入hotseat,你的workspace只能修改为5*2的配置,你需要在竖向空间流出一行的空间给hotseat使用。 2、Hotseat构造函数到这里基本上配置已经设置好。不过显示出来的效果并不是我们想象的结果,因为Hotseat内部对横向和竖向屏幕做了处理,我们需要做些修改。Launcher里面有专门管理Hotseat的类:Hotseat.java 。下面我们看看Hotseat.java的构造:
  1. public Hotseat(Context context, AttributeSet attrs, int defStyle) 
  2.   {
  3.         super(context, attrs, defStyle);

  4.         TypedArray a = context.obtainStyledAttributes(attrs,
  5.                 R.styleable.Hotseat, defStyle, 0);
  6.         mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
  7.         mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
  8.         mIsLandscape = context.getResources().getConfiguration().orientation ==
  9.             Configuration.ORIENTATION_LANDSCAPE;
  10.         
  11.         //设置成竖屏,使用竖屏时候的hotseat
  12.         mIsLandscape = false;
  13.     }
复制代码
注意这里有一个大屏幕还是小屏幕的判断,这个是用来判断属于平板系统还是一般的手机系统。因为我系统是只会在横屏时使用,所以我直接设置成mIsLandscape为小屏幕,因为Hotseat里面很多获取熟悉都是区分大小屏幕。小屏幕的时候,我们使用竖向配置hotseat就可以得到相当于手机系统的hotseat效果,hotseat会显示在屏幕底下。基本上修改上面几个地方,就可以在平板屏幕底下显示hotseat。下面我们分析一下Hotseat是如何实现的。 3、Hotseat加载数据Hotseat加载数据可以分为两部分,AllApp按钮和其他默认按钮。下面我们先看看其他默认按钮是如何加载的:默认按钮加载跟workspace的默认数据加载一样,都是在LauncherModel加载。Hotseat和workspace的app类型加载方式一样,
  1. private void loadWorkspace() 
  2. {
  3.   //........
  4.      switch (container) 
  5.     {  
  6.         case LauncherSettings.Favorites.CONTAINER_DESKTOP:  
  7.         case LauncherSettings.Favorites.CONTAINER_HOTSEAT:  
  8.              sWorkspaceItems.add(info);  
  9.              break;  
  10.     //........
  11. }
复制代码
上面是上一篇我们分析Launcher加载初始化数据的部分代码,我们可以看到,Hotseat的数据加载跟workspace的一般APP快捷方式加载是一样的,而且他们共用一个队列保存数据。具体数据加载过程分析可以查看我上一篇文章。 4、Hotseat绑定数据 hotseat绑定数据跟workspace流程基本一样,下面是hotseat开始绑定时,调用了Hotseat自身的方法清空数据。
  1. public void startBinding() 
  2. {  
  3.    //..........
  4.    // 清空Hotseat的数据
  5.     if (mHotseat != null) 
  6.   {  
  7.         mHotseat.resetLayout();  
  8.     }  
  9. }
复制代码
上面配置文件分析的时候,我们也说了Hotseat里面其实也是一个CellLayout负责管理内部的元素,下面我们看看它如何绑定数据到CellLayout。在workspace.java类里面的addInScreen()方法实现。
  1. void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY,
  2.             boolean insert) {
  3.     //...........

  4.     //创建CellLayout,用于添加Hotseat的对象。
  5.         final CellLayout layout;
  6.         if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
  7.             layout = mLauncher.getHotseat().getLayout();
  8.             child.setOnKeyListener(null);

  9.             // Hide folder title in the hotseat
  10.             if (child instanceof FolderIcon) {
  11.                 ((FolderIcon) child).setTextVisible(false);
  12.             }

  13.             if (screen < 0) {
  14.                 screen = mLauncher.getHotseat().getOrderInHotseat(x, y);
  15.             } else {
  16.                 // Note: We do this to ensure that the hotseat is always laid out in the orientation
  17.                 // of the hotseat in order regardless of which orientation they were added
  18.                 //获取child的位置,返回true添加成功,false失败  
  19.                 x = mLauncher.getHotseat().getCellXFromOrder(screen);
  20.                 y = mLauncher.getHotseat().getCellYFromOrder(screen);
  21.             }
  22.         } else {
  23.             //如果Hotseat里面有Folder,隐藏文件夹名字
  24.             if (child instanceof FolderIcon) {
  25.                 ((FolderIcon) child).setTextVisible(true);
  26.             }

  27.             layout = (CellLayout) getChildAt(screen);
  28.             child.setOnKeyListener(new IconKeyEventListener());
  29.         }

  30.      //.........
  31.     }
复制代码
这里只给出Hotseat关键的添加代码,其他一些相关的内容,可以查看源码。 5、Hotseat类Hotseat类里面其实东西不多,主要就是我们上面说的构造函数,另外还有下面的设置AllAPP按钮的方法。
  1. void resetLayout() {
  2.     //清空原来的内容
  3.         mContent.removeAllViewsInLayout();

  4.         //添加AllAPP按钮,也是一个BubbleTextView对象
  5.         Context context = getContext();
  6.         LayoutInflater inflater = LayoutInflater.from(context);
  7.         BubbleTextView allAppsButton = (BubbleTextView)
  8.                 inflater.inflate(R.layout.application, mContent, false);
  9.      //加载AllAPP按钮的图标,这里使用了selector作为按钮配置
  10.         allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
  11.                 context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
  12.         // allAppsButton.setText(context.getString(R.string.all_apps_button_label));
  13.         allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
  14.         //allapp按钮触摸和点击响应,回调Launcher的功能
  15.         allAppsButton.setOnTouchListener(new View.OnTouchListener() 
  16.         {
  17.             @Override
  18.             public boolean onTouch(View v, MotionEvent event) {
  19.                 if (mLauncher != null &&
  20.                     (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
  21.                     mLauncher.onTouchDownAllAppsButton(v);
  22.                 }
  23.                 return false;
  24.             }
  25.         });
  26.      //AllAPP按钮点击响应的方法,点击的处理在Launcher类里面
  27.         allAppsButton.setOnClickListener(new View.OnClickListener() 
  28.         {
  29.             @Override
  30.             public void onClick(android.view.View v) {
  31.                 if (mLauncher != null) {
  32.                     mLauncher.onClickAllAppsButton(v);
  33.                 }
  34.             }
  35.         });

  36.      //这里会判断是小屏幕还是大屏幕,决定AllAPP按钮的位置
  37.         int x = getCellXFromOrder(sAllAppsButtonRank);
  38.         int y = getCellYFromOrder(sAllAppsButtonRank);
  39.         Log.d("Mythou_Launcher", "Hotseat------>x="+x+"  y="+y);
  40.         //Hotseat中清空了装载的内容,然后重新加载allAppsButton到CellLayout mythou
  41.         mContent.addViewToCellLayout(allAppsButton, -1, 0, new CellLayout.LayoutParams(x,y,1,1),
  42.                 true);
  43.     }
复制代码
Hotseat里面其他几个简单方法,基本上都是获取一些属性,这里就不详细分析。6、总结
  • Hotseat其实也是一个CellLayout负责管理里面的所有数据。
  • 大部分配置可以通过XML配置文件修改得到。
  • 加载和绑定数据和workspace基本是一致的。



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信Q币充错怎么办 淘宝q币充错了怎么办 移动充值错误怎么办 qq忘记密码怎么办登陆 sql数据库满了怎么办 微信充值话费不到账怎么办 填报志愿密码错误怎么办 校园卡被冻结了怎么办 充值卡密码丢了怎么办 联通手机号码不支持开黄钻怎么办 手机qq加载失败怎么办 qq红包额度上限怎么办 qq红包超出限额怎么办 qq红包超过限额怎么办 红包过期没退回怎么办 qb充值错了号码怎么办 天猫q币充值被骗怎么办 微信充错了q币怎么办 qq钱包没银行卡怎么办 qq钱包被限制怎么办 充流量未到账怎么办 云客服被冻结怎么办 联通话费注销了怎么办 我的话费太多怎么办 lol充错账号怎么办 支付宝手机充值没到账怎么办 支付宝限额10000怎么办 米币超过限制怎么办 忘记财付通支付密码怎么办 支付宝忘记登陆密码怎么办 三位数密码忘记了怎么办 登入密码忘记怎么办 电脑登录忘记密码怎么办 支付宝忘记密码怎么办 密码箱忘了密码怎么办 信用卡忘了密码怎么办 我没有财付通钱没有了怎么办 发红包忘记密码怎么办 手机怎么办理财付通 忘记qq红包密码怎么办 财付通自动扣费怎么办