Android 14.0 Launcher3定制化之桌面分页横线改成圆点显示功能实现

发布于:2024-03-03 ⋅ 阅读:(121) ⋅ 点赞:(0)

1.前言

在14.0的系统rom产品定制化开发中,在进行launcher3的定制化中,在双层改为单层的开发中,在原生的分页
是横线,而为了美观就采用了系统原来的另外一种分页方式,就是圆点比较美观,接下来就来分析下相关的实现,然后实现其功能

2.Launcher3定制化之桌面分页横线改成圆点显示功能实现的核心类

    packages/apps/Launcher3/res/layout/launcher.xml
    packages/apps/Launcher3/src/com/android/launcher3/pageindicators/PageIndicatorDots.java

3.Launcher3定制化之桌面分页横线改成圆点显示功能实现的核心功能分析和实现

在Launcher3中的核心布局中,最核心的就是workspace hotseat folder等核心部件中,在
Launcher3中的核心布局的相关控件就是在launcher.xml中这个Launcher主页面中显示的,这里显示Launcher3桌面的核心控件,
DragLayer Workspace WorkspacePageIndicatorLine drop_target_bar hotseat等主要控件的布局
在这里面WorkspacePageIndicatorLine就是所谓的分页横线控件,而PageIndicatorDots.java就是
Launcher3的另外一种分页圆点显示控件,所以接下来分析下launcher.xml的相关源码

3.1 launcher.xml中相关部件源码分析

在实现Launcher3定制化之桌面分页横线改成圆点显示功能实现的核心功能中,通过上述的分析得知,
在launcher.xml中的相关源码中这里就是关于桌面布局的核心布局文件,接下来看下相关源码修改

   <com.android.launcher3.LauncherRootView
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:launcher="http://schemas.android.com/apk/res-auto"
         android:id="@+id/launcher"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true">
     
         <com.android.launcher3.dragndrop.DragLayer
             android:id="@+id/drag_layer"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:clipChildren="false"
             android:clipToPadding="false"
             android:importantForAccessibility="no">
     
             <com.android.launcher3.views.AccessibilityActionsView
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:contentDescription="@string/home_screen"
                 />
     
             <!-- The workspace contains 5 screens of cells -->
             <!-- DO NOT CHANGE THE ID -->
             <com.android.launcher3.Workspace
                 android:id="@+id/workspace"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:layout_gravity="center"
                 android:theme="@style/HomeScreenElementTheme"
                 launcher:pageIndicator="@+id/page_indicator" />
     
             <!-- DO NOT CHANGE THE ID -->
             <include
                 android:id="@+id/hotseat"
                 layout="@layout/hotseat" />
     
    -        <com.sprd.ext.pageindicators.WorkspacePageIndicatorLine
    +        <com.android.launcher3.pageindicators.PageIndicatorDots
                 android:id="@+id/page_indicator"
                 android:layout_width="match_parent"
                 android:layout_height="@dimen/workspace_page_indicator_height"
                 android:layout_gravity="bottom|center_horizontal"
                 android:theme="@style/HomeScreenElementTheme" />
     
             <include
                 android:id="@+id/drop_target_bar"
                 layout="@layout/drop_target_bar" />
     
             <com.android.launcher3.views.ScrimView
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:id="@+id/scrim_view"
                 android:background="@android:color/transparent" />
     
             <include
                 android:id="@+id/apps_view"
                 layout="@layout/all_apps"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent" />
     
             <include
                 android:id="@+id/overview_panel"
                 layout="@layout/overview_panel" />
     
         </com.android.launcher3.dragndrop.DragLayer>
     
     </com.android.launcher3.LauncherRootView>

通过上述的Launcher3中的核心主屏幕部件launcher.xml中的相关源码分析得知,在这里
Launcher.xml中的布局文件中,com.android.launcher3.pageindicators.WorkspacePageIndicator
就是Launcher3主屏幕的workspaces的布局分页横线控件的显示分页的,所以需要改成
圆点分页控件就需要修改为com.android.launcher3.pageindicators.PageIndicatorDots
这样通过上述的修改,就完成了修改为Launcher3布局分页圆点显示的功能的修改,接下来
看下其他方面关于横线替换成圆点的相关修改,接下来分析下PageIndicatorDots.java
中的相关源码实现

3.2 PageIndicatorDots中关于实现圆点绘制布局的相关源码的修改

通过上述的Launcher3中的核心控件的分析 和以前在10.0到12.0的相关修改横线分页到
圆点分页都是需要实现 setInsets(Rect insets)等相关方法的,通过这些的修改来
完成对圆点分页的适配工作,接下来具体分析下PageIndicatorDots中的相关源码布局
通过在Insettable中的接口实现 setInsets(Rect insets)来在这个类里面添加具体的方法

    import com.android.launcher3.R;
     import com.android.launcher3.Utilities;
     import com.android.launcher3.util.Themes;
    -
    +import com.android.launcher3.Launcher;
    +import android.graphics.Rect;
    +import com.android.launcher3.DeviceProfile;
    +import android.view.Gravity;
    +import android.widget.FrameLayout;
    +import com.android.launcher3.Insettable;

    -public class PageIndicatorDots extends View implements PageIndicator {
    +public class PageIndicatorDots extends View implements Insettable,PageIndicator {
         private float mCurrentPosition;
         private float mFinalPosition;
         private ObjectAnimator mAnimator;
    -
    +    private Launcher mLauncher;
         private float[] mEntryAnimationRadiusFactors;
     
         public PageIndicatorDots(Context context) {
    @@ -114,7 +119,7 @@ public class PageIndicatorDots extends View implements PageIndicator {
             mCirclePaint.setColor(Themes.getAttrColor(context, R.attr.folderPaginationColor));
             mDotRadius = getResources().getDimension(R.dimen.page_indicator_dot_size) / 2;
             setOutlineProvider(new MyOutlineProver());
    -
    +        mLauncher = Launcher.getLauncher(context);
             mIsRtl = Utilities.isRtl(getResources());
         }

Launcher3定制化之桌面分页横线改成圆点显示功能实现的核心功能中,通过上述的分析得知,
在上述的PageIndicatorDots方法中,通过实现Insettable的接口,接下来就可以在
这里添加setInsets(Rect insets)的实现,通过需要实例化Launcher的实现,在实现
setInsets(Rect insets)的时候用到相关的实现具体实现如下

  +    @Override
    +    public void setInsets(Rect insets) {
    +        DeviceProfile grid = mLauncher.getDeviceProfile();
    +        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
    +
    +        if (grid.isVerticalBarLayout()) {
    +            Rect padding = grid.workspacePadding;
    +            lp.leftMargin = padding.left + grid.workspaceCellPaddingXPx;
    +            lp.rightMargin = padding.right + grid.workspaceCellPaddingXPx;
    +            lp.bottomMargin = padding.bottom;
    +        } else {
    +            lp.leftMargin = lp.rightMargin = 0;
    +            lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
    +            lp.bottomMargin = grid.hotseatBarSizePx + insets.bottom;
    +        }
    +        setLayoutParams(lp);
    +    }

在通过在使用的过程中,会发现在计算分页数量的时候,这里会出现异常,所以需要在
异常的地方做修改,排除分页滚动为0的情况,具体分析实现如下:

  @Override
      public void setScroll(int currentScroll, int totalScroll) {
          if (SHOW_DOT_PAGINATION.get() && mActivePage != 0 && currentScroll == 0) {
              CURRENT_POSITION.set(this, (float) mActivePage);
              return;
          }
  
          if (mNumPages <= 1) {
              return;
          }
  
          if (mShouldAutoHide) {
              animatePaginationToAlpha(VISIBLE_ALPHA);
          }
  
          if (mIsRtl) {
              currentScroll = totalScroll - currentScroll;
          }
                 int scrollPerPage = totalScroll / (mNumPages - 1);
    +            if(scrollPerPage == 0)return;
                 int pageToLeft = currentScroll / scrollPerPage;
                 int pageToLeftScroll = pageToLeft * scrollPerPage;
                 int pageToRightScroll = pageToLeftScroll + scrollPerPage;
    @@ -336,4 +342,22 @@ public class PageIndicatorDots extends View implements PageIndicator {
                 }
             }
         }

通过上面几部分的修改,在Launcher3中,就完美实现了在双层改成单层的时候,关于分页横线
修改为分页圆点的核心功能的实现,最终就完成了功能的实现

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到