原生的launcher的Hotseat如下图(1)所示,我想把效果改成图(2)
图(1)
图(2)
一:定制化HotSeat
修改的类:packages/apps/Launcher3/com/android/launcher3/Hotseat.java
(1).修改hotseat的宽 Hotseat------->setInsets
@Override
public void setInsets(Rect insets) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
DeviceProfile grid = mActivity.getDeviceProfile();//这是拖拽的是时候预览位图判断
if (grid.isVerticalBarLayout()) {
mQsb.setVisibility(View.GONE);
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
if (grid.isSeascape()) {
lp.gravity = Gravity.LEFT;
lp.width = grid.hotseatBarSizePx + insets.left;
} else {
lp.gravity = Gravity.RIGHT;
lp.width = grid.hotseatBarSizePx + insets.right;
}
} else {
mQsb.setVisibility(View.VISIBLE);
//lp.gravity = Gravity.BOTTOM;//注释原本的
//这两句不用管,下面会更改
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = grid.hotseatBarSizePx;
Log.e("TAG","Hotseat with=========================="+grid.hotseatBarSizePx);
//以下是新增的代码
lp.gravity = Gravity.BOTTOM | Gravity.CENTER;
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
Log.e("TAG","Hotseat screenWidth=========================="+screenWidth);
// 计算80%的宽度
int desiredWidth = (int) (screenWidth * 0.8);
Log.e("TAG","Hotseat desiredWidth=========================="+desiredWidth);
lp.width = desiredWidth;
}
//添加这一句把位置拉上去
lp.bottomMargin = insets.bottom;
}
Rect padding = grid.getHotseatLayoutPadding(getContext());
//setPadding(padding.left, padding.top, padding.right, padding.bottom);
//因为底部的hotseat是占用全部的,所有这里我们把位置调节一下
setPadding( (insets.bottom / 4), 0, (insets.bottom / 4), 0);
setLayoutParams(lp);
InsettableFrameLayout.dispatchInsets(this, insets);
}
(2).修改hotseat的背景色,有个定制化颜色的框 这里我们用颜色来替代
drawable------>hotseat_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33FFFFFF"/>
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
暂时修改为:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#80FFFFFF"/>
<corners android:radius="20dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
最后再在Hotseat初始化的地方添加:setBackgroundResource(R.drawable.hotseat_bg);
二、替换图标(这里的图标我是直接放在mipmap里面)
(1).直接替换某些应用图标
\packages\apps\Launcher3\src\com\android\launcher3\BubbleTextView\packages\apps\Launcher3\src\com\android\launcher3\BubbleTextView.java
//先导包
import com.android.launcher3.icons.LauncherIcons;
import android.graphics.Bitmap;
import com.android.launcher3.icons.BitmapInfo;
import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import androidx.core.content.res.ResourcesCompat;
import android.util.Log;
//放入图片到mipmap
//根据包名替换图片,新增方法convertBitmapInfoSpecial
public BitmapInfo convertBitmapInfoSpecial(String packname){
Resources resources = getContext().getResources();
Drawable drawable = null;
try {
if (packname.equals("com.android.calculator2")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("calculator_icon", "mipmap", getContext().getPackageName()), null);
} else if (packname.equals("com.android.soundrecorder")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("soundrecorder_icon", "mipmap", getContext().getPackageName()), null);
} else if (packname.equals("com.android.calendar")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("calendar_icon", "mipmap", getContext().getPackageName()), null);
} else if (packname.equals("com.android.settings")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("settings_icon", "mipmap", getContext().getPackageName()), null);
} else if (packname.equals("com.android.camera2")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("camera_icon", "mipmap", getContext().getPackageName()), null);
} else if (packname.equals("com.android.music")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("music_icon", "mipmap", getContext().getPackageName()), null);
} else if (packname.equals("com.android.gallery3d")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("gallery3d_icon", "mipmap", getContext().getPackageName()), null);
}
if(drawable!=null){
Bitmap bitmap = drawableToBitmap(drawable);
LauncherIcons li = LauncherIcons.obtain(getContext());
return li.createIconBitmap(bitmap);
}else{
return null;
}
} catch (Exception e) {
Log.e("TAG", "没有获取到对应的包名======");
}
return null;
}
//在applyIconAndLabel里面替换图片
@UiThread
protected void applyIconAndLabel(ItemInfoWithIcon info) {
int flags = shouldUseTheme() ? FLAG_THEMED : 0;
if (mHideBadge) {
flags |= FLAG_NO_BADGE;
}
//这几句是新增的 用于替换图片用
BitmapInfo bitmapInfo = convertBitmapInfoSpecial(info.getTargetComponent().getPackageName());
if (bitmapInfo != null){
info.bitmap = bitmapInfo;
}
FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
mDotParams.appColor = iconDrawable.getIconColor();
mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
setIcon(iconDrawable);
applyLabel(info);
}
(2).第一步替换图片以后,发现在按住移动的时候会还原,那么我们找到按住显示图片的地方再修改一次
\packages\apps\Launcher3\src\com\android\launcher3\Utilities.java
//先导包
import com.android.launcher3.icons.LauncherIcons;
import android.graphics.Bitmap;
import com.android.launcher3.icons.BitmapInfo;
import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import androidx.core.content.res.ResourcesCompat;
import android.util.Log;
//根据包名替换图片,新增方法getDrawableSpecial
public static Drawable getDrawableSpecial(Context context,String pckName) {
Resources resources = context.getResources();
Drawable drawable = null;
try {
if (pckName.equals("com.android.calculator2")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("calculator_icon", "mipmap", context.getPackageName()), null);
} else if (pckName.equals("com.android.soundrecorder")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("soundrecorder_icon", "mipmap", context.getPackageName()), null);
} else if (pckName.equals("com.android.calendar")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("calendar_icon", "mipmap", context.getPackageName()), null);
} else if (pckName.equals("com.android.settings")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("settings_icon", "mipmap", context.getPackageName()), null);
} else if (pckName.equals("com.android.camera2")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("camera_icon", "mipmap", context.getPackageName()), null);
} else if (pckName.equals("com.android.music")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("music_icon", "mipmap", context.getPackageName()), null);
} else if (pckName.equals("com.android.gallery3d")) {
drawable = ResourcesCompat.getDrawable(resources, resources.getIdentifier("gallery3d_icon", "mipmap", context.getPackageName()), null);
}
return drawable;
} catch (Exception e) {
Log.e("TAG", "没有获取到对应的包名======");
}
return null;
}
//然后在loadFullDrawableWithoutTheme里面修改
if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
LauncherActivityInfo activityInfo = context.getSystemService(LauncherApps.class)
.resolveActivity(info.getIntent(), info.user);
outObj[0] = activityInfo;
//这就是新增的
Drawable drawableSpecial = getDrawableSpecial(context,info.getTargetComponent().getPackageName());
if (drawableSpecial == null){
//这里是原本的代码,放到这里
return activityInfo == null ? null : LauncherAppState.getInstance(context)
.getIconProvider().getIcon(
activityInfo, activity.getDeviceProfile().inv.fillResIconDpi);
}else{
//如果找到了对应的包名,那么我们就替换图标
return drawableSpecial;
}
//这里是原本的代码 我们先备份注释一份
//return activityInfo == null ? null : LauncherAppState.getInstance(context)
// .getIconProvider().getIcon(
// activityInfo, activity.getDeviceProfile().inv.fillResIconDpi);
}
这时候图标不管移动还是显示都是正常的,但是替换图标以后会变大,我们尝试更改
\frameworks\libs\systemui\iconloaderlib\src\com\android\launcher3\icons\BaseIconFactory.java
public BitmapInfo createIconBitmap(Bitmap icon) {
Log.e("TAG","createIconBitmap===============================================0.8");
if (mIconBitmapSize != icon.getWidth() || mIconBitmapSize != icon.getHeight()) {
//这里本来是1.0f的,我们改成0.8f
icon = createIconBitmap(new BitmapDrawable(mContext.getResources(), icon), 0.8f);
}
return BitmapInfo.of(icon, mColorExtractor.findDominantColorByHue(icon));
}
三:默认桌面模式(14的launcher3有抽屉模式以及桌面模式)
修改的类:
vendor/sprd/platform/packages/apps/Launcher3/res_unisoc/values/config_ext.xml
因为安卓14的launcher3支持桌面模式以及抽屉模式切换,所以我们默认为桌面模式更改
//修改的这个属性来设置抽屉还是没有抽屉模式 dual为抽屉模式 single为桌面模式
<!--The value must be dual or single-->
<string name="default_home_screen_style" translatable="false">single</string>