当前位置:   article > 正文

Android 10.0 Launcher3拖拽图标进入hotseat自适应布局功能实现一

Android 10.0 Launcher3拖拽图标进入hotseat自适应布局功能实现一

1.前言

在10.0的系统rom定制化开发中,在对于launcher3的一些开发定制中,在对hotseat的一些开发中,需要实现动态hotseat居中
的功能,就是在拖拽图标进入和拖出hotseat,都可以保持hotseat居中的功能,接下来分析下相关功能实现
具体如图:


2.Launcher3拖拽图标进入hotseat自适应布局功能实现一的核心类

packages\apps\Launcher3\src\com\android\launcher3\Hotseat.java

3.Launcher3拖拽图标进入hotseat自适应布局功能实现一的核心功能分析和实现

Launcher顾名思义,就是桌面的意思,也是android系统启动后第一个启动的应用程序,
:Launcher3负责管理和展示用户手机桌面上的各个应用程序图标。它通过GridView或者LinearLayout等布局管理器将
图标进行排列,并支持滑动、放大缩小等手势操作
Hotseat也是属于在导航栏底部的BubbleTextView的布局,只是不显示app图标

3.1 Hotseat.java相关添加背景功能分析

在实现Launcher3拖拽图标进入hotseat自适应布局功能实现一的核心功能中,通过上述的分析得知,
首选需要给Hotseat添加背景功能,然后需要根据hotseat的数量多少来设置hotseat的宽度高度等
相关参数,这样就实现了第一步的hotseat的居中显示功能,

  1. public class Hotseat extends CellLayout implements LogContainerProvider, Insettable, Transposable {
  2. @ViewDebug.ExportedProperty(category = "launcher")
  3. public boolean mHasVerticalHotseat;
  4. private final HotseatController mController;
  5. public Hotseat(Context context) {
  6. this(context, null);
  7. }
  8. public Hotseat(Context context, AttributeSet attrs) {
  9. this(context, attrs, 0);
  10. }
  11. public Hotseat(Context context, AttributeSet attrs, int defStyle) {
  12. super(context, attrs, defStyle);
  13. mController = LauncherAppMonitor.getInstance(context).getHotseatController();
  14. }
  15. public HotseatController getController() {
  16. return mController;
  17. }
  18. /* Get the orientation specific coordinates given an invariant order in the hotseat. */
  19. public int getCellXFromOrder(int rank) {
  20. return mHasVerticalHotseat ? 0 : rank;
  21. }
  22. public int getCellYFromOrder(int rank) {
  23. return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
  24. }
  25. public void resetLayout(boolean hasVerticalHotseat) {
  26. removeAllViewsInLayout();
  27. mHasVerticalHotseat = hasVerticalHotseat;
  28. InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
  29. if (hasVerticalHotseat) {
  30. setGridSize(1, idp.numHotseatIcons);
  31. } else {
  32. setGridSize(idp.numHotseatIcons, 1);
  33. }
  34. //add core start
  35. // 添加背景
  36. if(idp.numHotseatIcons>0){
  37. setBackgroundResource(R.drawable.shape_corner);
  38. }
  39. //add core end
  40. }
  41. @Override
  42. public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
  43. target.gridX = info.cellX;
  44. target.gridY = info.cellY;
  45. targetParent.containerType = LauncherLogProto.ContainerType.HOTSEAT;
  46. }
  47. @Override
  48. public void setInsets(Rect insets) {
  49. FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
  50. DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
  51. insets = grid.getInsets();
  52. if (grid.isVerticalBarLayout()) {
  53. lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
  54. if (grid.isSeascape()) {
  55. lp.gravity = Gravity.LEFT;
  56. lp.width = grid.hotseatBarSizePx + insets.left;
  57. } else {
  58. lp.gravity = Gravity.RIGHT;
  59. lp.width = grid.hotseatBarSizePx + insets.right;
  60. }
  61. } else {
  62. lp.gravity = Gravity.BOTTOM;
  63. lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
  64. lp.height = grid.hotseatBarSizePx + insets.bottom;
  65. }
  66. Rect padding = grid.getHotseatLayoutPadding();
  67. setPadding(padding.left, padding.top, padding.right, padding.bottom);
  68. setLayoutParams(lp);
  69. InsettableFrameLayout.dispatchInsets(this, insets);
  70. }
  71. @Override
  72. public boolean onTouchEvent(MotionEvent event) {
  73. // Don't let if follow through to workspace
  74. return true;
  75. }
  76. @Override
  77. public RotationMode getRotationMode() {
  78. return Launcher.getLauncher(getContext()).getRotationMode();
  79. }
  80. }

在实现Launcher3拖拽图标进入hotseat自适应布局功能实现一的核心功能中,通过上述的分析得知,
在Hotseat中相关源码分析,可以发现由
resetLayout 就是负责布局的 当hotseat 增加减少时都会重新布局
所以在setBackgroundResource(R.drawable.shape_corner);添加背景就可以了

  1. public void resetLayout(boolean hasVerticalHotseat) {
  2. removeAllViewsInLayout();
  3. mHasVerticalHotseat = hasVerticalHotseat;
  4. InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
  5. if (hasVerticalHotseat) {
  6. setGridSize(1, idp.numHotseatIcons);
  7. } else {
  8. setGridSize(idp.numHotseatIcons, 1);
  9. }
  10. // 添加背景
  11. if(idp.numHotseatIcons>0){
  12. setBackgroundResource(R.drawable.shape_corner);
  13. }
  14. }
  15. shape_corner.xml
  16. <?xml version="1.0" encoding="utf-8"?>
  17. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  18. <!--背景颜色-->
  19. <solid android:color="#FFFAFA" />
  20. <!--角的半径-->
  21. <corners android:radius="10dp"/>
  22. <!--边框颜色-->
  23. <stroke android:width="1dp" android:color="#00000000" />
  24. </shape>
  25. public void setInsets(Rect insets) {
  26. FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
  27. DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
  28. insets = grid.getInsets();
  29. //竖屏布局
  30. if (grid.isVerticalBarLayout()) {
  31. lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
  32. if (grid.isSeascape()) {
  33. lp.gravity = Gravity.LEFT;
  34. lp.width = grid.hotseatBarSizePx + insets.left;
  35. } else {
  36. lp.gravity = Gravity.RIGHT;
  37. lp.width = grid.hotseatBarSizePx + insets.right;
  38. }
  39. } else {
  40. //modify core start
  41. // 横屏布局
  42. // 平板开发项目 固定横屏,所以要在这里设置参数
  43. // 设置宽高 左边底部的间距
  44. InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
  45. int hotseatNums = idp.numHotseatIcons;
  46. lp.width = hotseatNums*grid.hotseatBarSizePx+(hotseatNums+1)*dip2px(15.0f);
  47. lp.height = grid.hotseatBarSizePx + insets.bottom;
  48. if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
  49. lp.leftMargin = (int)((1080-lp.width)/2);
  50. }else{
  51. lp.leftMargin = (int)((1920-lp.width)/2);
  52. }
  53. lp.gravity = Gravity.BOTTOM;
  54. //modify core end
  55. }
  56. Rect padding = grid.getHotseatLayoutPadding();
  57. // 设置padding 布局
  58. setPadding(0, padding.top, 0,0);
  59. setLayoutParams(lp);
  60. InsettableFrameLayout.dispatchInsets(this, insets);
  61. }

在实现Launcher3拖拽图标进入hotseat自适应布局功能实现一的核心功能中,通过上述的分析得知,
在Hotseat中相关源码分析,
而setInset() 负责设置绘制布局 的参数 这里设置hotseat的宽高等参数布局
其实只需要修改lp的参数就行了 然后hotseat 会根据长宽等参数 来具体布局每一个hotseat的具体坐标
根据横竖屏来确定lp.leftMargin的值,就可以保证居中显示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/850282
推荐阅读
相关标签
  

闽ICP备14008679号