前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发中Launcher3常见默认配置修改方法总结

Android开发中Launcher3常见默认配置修改方法总结

作者头像
砸漏
发布2020-10-27 20:24:44
3.5K0
发布2020-10-27 20:24:44
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例讲述了Android开发中Launcher3常见默认配置修改方法。分享给大家供大家参考,具体如下:

Launcher概述

Launcher是开机完成后第一个启动的应用,用来展示应用列表和快捷方式、小部件等。Launcher作为第一个(开机后第一个启动的应用)展示给用户的应用程序,其设计的好坏影响到用户的体验,甚至影响用户购机的判断。所以很多品牌厂商都会不遗余力的对Launcher进行深度定制,如小米的MIUI、华为的EMUI等。Android默认的Launcher没有过多的定制,更加简洁,受到源生党的追捧,Google的Nexus系列手机基本都是用的源生Launcher,目前Android源生的Launcher版本是Launcher3,后面的相关内容也都是以Launcher3为基础。

Launcher3默认配置修改

1.如何设置默认页

res/values/Config.xml

代码语言:javascript
复制
<integer name="config_workspaceDefaultScreen" 0</integer 

在Launcher3 桌面,不管在哪一页,按HOME 键,会回到默认页。

2.如何隐藏launcher3中的搜索框

① 在Launcher3/src/com/android/launcher3/Launcher.java中

注释updateGlobalIcons()方法调用,共两处。

代码语言:javascript
复制
public View getQsbBar() {
  if (mQsbBar == null) {
    mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
-       mSearchDropTargetBar.addView(mQsbBar);
  }
+    mQsbBar.setVisibility(View.GONE);
  return mQsbBar;
}
@Override
public void bindSearchablesChanged() { //注释该方法内容
/*    boolean searchVisible = updateGlobalSearchIcon();
  boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
  if (mSearchDropTargetBar != null) {
    mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
  }
*/
}

② 在Launcher3/src/com/android/launcher3/DynamicGrid.java中

代码语言:javascript
复制
// Layout the search bar
  //注释如下内容
/*    View qsbBar = launcher.getQsbBar();
  LayoutParams vglp = qsbBar.getLayoutParams();
  vglp.width = LayoutParams.MATCH_PARENT;
  vglp.height = LayoutParams.MATCH_PARENT;
  qsbBar.setLayoutParams(vglp);
*/

③ 在Launcher3/res/values/dimens.xml中 – <dimen name=”dynamic_grid_search_bar_height” 48dp</dimen + <dimen name=”dynamic_grid_search_bar_height” 18dp</dimen

重新编译后即可看到效果。

3.如何调整原生Launcher3主界面的search框的大小?

修改如下:

定位打/packages/apps/Launcher3/res/values/dimens.xml。

<dimen name=”dynamic_grid_edge_margin” 3dp</dimen //修改这个可以调整search框距离顶部距离。 <dimen name=”dynamic_grid_search_bar_max_width” 500dp</dimen //search框的宽度,一般不需要调整。 <dimen name=”dynamic_grid_search_bar_height” 48dp</dimen //search框的高度,不要调整为0,删除按钮需要占用一部分空间。

4.让主菜单部分应用按指定顺序排在前面?

添加res/values/arrays.xml:需要排序的应用:这里的item 内容一定要填写正确,否则会匹配不上,无法参与排序。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"? 
<resources 
<string-array name="apps_componentName" translatable="false" 
  <item ComponentInfo{com.android.vending/com.android.vending.AssetBrowserActivity}</item 
  <item ComponentInfo{com.android.browser/com.android.browser.BrowserActivity}</item 
  <item ComponentInfo{com.android.settings/com.android.settings.Settings}</item 
  <item ComponentInfo{com.android.camera2/com.android.camera.CameraLauncher}</item 
  <item ComponentInfo{com.android.mms/com.android.mms.ui.ConversationList}</item 
</string-array 
</resources 

src/com/android/launcher3/Utilities.java

代码语言:javascript
复制
 import java.util.Arrays;
 import java.util.List;
  public static List<String  getAppsComponentName(final Context context) {
    return Arrays.asList(context.getResources().getStringArray(R.array.apps_componentName));
  }
src/com/android/launcher3/LauncherModel.java
 protected int mPreviousConfigMcc;
  static List<String  appArray = new ArrayList<String ();
  LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
    ......
    mUserManager = UserManagerCompat.getInstance(context);
    appArray = Utilities.getAppsComponentName(context);
  }

添加如下sortApps 方法:apps 按arrays.xml 排序,在原来的排序基础上,将arrays.xml 配置的应用按顺序排在前面。arrays.xml中没有涉及到的应用,还是原来的顺序。

代码语言:javascript
复制
public static final void sortApps(ArrayList<AppInfo  apps) {
  int length = appArray.size();
  List<AppInfo  assignApps = new ArrayList<AppInfo ();
  for(int i=0;i<length;i++) {
    assignApps.add(i, null);
  }
  for(AppInfo app : apps){
    for(int k=0; k<length; k++){
      if (app.componentName.toString().equals(appArray.get(k))) {
        assignApps.set(k,app );
        continue;
      }
    }
  }
  for (int i =length -1;i   -1 ;i--) {
   AppInfo app = assignApps.get(i);
   if(app != null){
     apps.remove(app);
     apps.add(0, app);
   }
 }
 Log.d(TAG ,"The Apps List after Sort!");
}

src/com/android/launcher3/AppsCustomizePagedView.java

代码语言:javascript
复制
public void setApps(ArrayList<AppInfo  list) {
    if (!LauncherAppState.isDisableAllApps()) {
      ......
      SprdAppSortAddonStub.getInstance().sortApps(mApps);
      LauncherModel.sortApps(mApps);//在原来排序的基础上,再将arrays.xml中配置的应用按顺序排在前面。
      updatePageCountsAndInvalidateData();
    }
}
private void addAppsWithoutInvalidate(ArrayList<AppInfo  list) {
    ......
    // SPRD: bug375932 2014-12-02 Feature customize app icon sort.
    SprdAppSortAddonStub.getInstance().sortApps(mApps);
    LauncherModel.sortApps(mApps);//在原来排序的基础上,再将arrays.xml中配置的应用按顺序排在前面。
}

5.如何确定待机HOME界面布局使用的是哪个default_workspace文件?

src/com/android/launcher3/DynamicGrid.java

选择哪个default_workspace 和public DynamicGrid(Context context, Resources resources,int minWidthPx, int minHeightPx, int widthPx, int heightPx, int awPx, int ahPx)中的minWidthPx 和minHeightPx 以及该方法中创建的deviceProfiles 列表关。 minWidthPx 、minHeightPx 值转换为dpi之后 ,deviceProfiles 列表与其进行比较,选择与当前屏幕大小最接近的deviceProfiles 的default_workSpace作为最终Home界面使用的default_workspace。

详细解释如下:

src/com/android/launcher3/DynamicGrid.java中

① deviceProfiles 列表如下:

代码语言:javascript
复制
deviceProfiles.add(new DeviceProfile("Super Short Stubby",
    255, 300, 2, 3, 48, 13, (hasAA ? 3 : 5), 48, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Shorter Stubby",
    255, 400, 3, 3, 48, 13, (hasAA ? 3 : 5), 48, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Short Stubby",
    275, 420, 3, 4, 48, 13, (hasAA ? 5 : 5), 48, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Stubby",
    255, 450, 3, 4, 48, 13, (hasAA ? 5 : 5), 48, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Nexus S",
    296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 5), 48, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Nexus 4",
    335, 567, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Nexus 5",
    359, 567, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56, R.xml.default_workspace_4x4));
deviceProfiles.add(new DeviceProfile("Large Phone",
    406, 694, 5, 5, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
// The tablet profile is odd in that the landscape orientation
// also includes the nav bar on the side
deviceProfiles.add(new DeviceProfile("Nexus 7",
    575, 904, 5, 6, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
// Larger tablet profiles always have system bars on the top & bottom
deviceProfiles.add(new DeviceProfile("Nexus 10",
    727, 1207, 5, 6, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
deviceProfiles.add(new DeviceProfile("20-inch Tablet",
    1527, 2527, 7, 7, 100, 20, 7, 72, R.xml.default_workspace_4x4));

② 重新计算MinWidth 和MinHeigh 单位是dpi。

代码语言:javascript
复制
mMinWidth = dpiFromPx(minWidthPx, dm);
mMinHeight = dpiFromPx(minHeightPx, dm);

③ 创建mProfile,mProfile.defaultLayoutId 就是最终Home界面使用的default_workspace 的id。

mProfile中的defaultLayoutId 是哪个default_workspace 见DeviceProfile.java。

代码语言:javascript
复制
mProfile = new DeviceProfile(context, deviceProfiles,
        mMinWidth, mMinHeight,
        widthPx, heightPx,
        awPx, ahPx,
        resources);
src/com/android/launcher3/DeviceProfile.java
  DeviceProfile(Context context,
         ArrayList<DeviceProfile  profiles,
         float minWidth, float minHeight,
         int wPx, int hPx,
         int awPx, int ahPx,
         Resources res) { 

方法中:

④ 用屏幕宽高创建的点(PointF xy = new PointF(width, height))与 deviceProfiles中的w 和 h 创建的点(dimens = new PointF(widthDps, heightDps))进行比较,也就是从deviceProfiles 列表中找出和当前屏幕大小最接近的deviceProfiles。

代码语言:javascript
复制
DeviceProfile closestProfile = findClosestDeviceProfile(minWidth, minHeight, points);
......

⑤ 采用和当前屏幕大小最接近的deviceProfiles的default_workspace

代码语言:javascript
复制
defaultLayoutId = closestProfile.defaultLayoutId; 

6.如何替换第三方应用在launcher上显示的图标?

在launcher/src/com/android/launcher3/IconCache.java中修改,

代码语言:javascript
复制
private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,  private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
    HashMap<Object, CharSequence  labelCache) {
  CacheEntry entry = mCache.get(componentName);
  if (entry == null) {
    entry = new CacheEntry();
    mCache.put(componentName, entry);
    ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
    if (labelCache != null && labelCache.containsKey(key)) {
      entry.title = labelCache.get(key).toString();
    } else {
      entry.title = info.loadLabel(mPackageManager).toString();
      if (labelCache != null) {
        labelCache.put(key, entry.title);
      }
    }
    if (entry.title == null) {
      entry.title = info.activityInfo.name;
    }
    Drawable icon;
    int index = sysIndexOf(componentName.getClassName());
    Log.i("jxt", "index:"+index+",Name:"+componentName.getClassName());
    icon = getFullResIcon(info);
    if (index  = 0) {
      entry.icon = Utilities.createIconBitmap(icon, mContext);
    } else {
      entry.icon = Utilities.createIconBitmap(
          /* SPRD: Feature 253522, Remove the application drawer view @{ */
          // getFullResIcon(info), mContext);
          icon, mContext, true);
    }
    /* 此处即为替换图标代码 {@*/
    if("第三方应用的componentName".equals(componentName.toString())){
      entry.icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.xxx);
    }
    /* @} */
  }
  return entry;
}  

7.如何去掉Launcher3的开机引导页面?

修改方案如下:

请定位到src/com/android/launcher3/LauncherClings.java文件:

代码语言:javascript
复制
class LauncherClings implements OnClickListener {
     ......
     private static final String TAG_CROP_TOP_AND_SIDES = "crop_bg_top_and_sides
    private static final boolean DISABLE_CLINGS = false;
    private static final boolean DISABLE_CLINGS = true;

8.为何Launcher3设置一些壁纸后,壁纸显示比预览图模糊?

预览的时候,没有做格式转化,所以显示正常!

在设置壁纸的时候,默认是采用jpeg格式转换的,导致转换后损耗了一些,设置壁纸后,某些对比度比较高的壁纸就显示的模糊!

修改方案:

默认修改为采用png格式转换!

android6.0之前的版本,请做如下修改:

定位到/packages/apps/Launcher3/的WallpaperCropActivity.java文件

1、String mOutputFormat = “jpg”;//修改为”png”

2、

代码语言:javascript
复制
protected static String getFileExtension(String requestFormat) {
    String outputFormat = (requestFormat == null)
        ? "jpg"//修改为"png"
        : requestFormat;
    outputFormat = outputFormat.toLowerCase();
    return (outputFormat.equals("png") || outputFormat.equals("gif"))
        ? "png" // We don't support gif compression.
        : "jpg";
}

android6.0的版本,请做如下修改:

定位到/packages/apps/Launcher3/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java文件

代码语言:javascript
复制
if (crop.compress(CompressFormat.JPEG, DEFAULT_COMPRESS_QUALITY, tmpOut))

修改为:

代码语言:javascript
复制
if (crop.compress(CompressFormat.PNG, DEFAULT_COMPRESS_QUALITY, tmpOut))

9. 6.0平台上Launcher3自带的壁纸路径是什么?

在6.0中,平台版本预置了一些壁纸资源,相关路径如下:

资源文件在:

packages/apps/Launcher3/WallpaperPicker/res/drawable-xhdpi/

字串文件在:

packages/apps/Launcher3/WallpaperPicker/res/values-nodpi/wallpapers.xml

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档