Android8.1 Launcher3 修改文件夹样式(一)
经过前面几天的努力,launcher看起来已经舒服多了;但是文件夹的外观还是让人不爽,不知道是不是因为美国人的审美跟我们不一样,还是Google工程师有意让我们自己修改。
这篇博客主要修改文件夹展开后的样式,博客理提到的文件夹都是展开后的文件夹。
1:首先让文件夹的大小固定
src/com/android/launcher3/folder/FolderPagedView.java -> FolderPagedView()
public FolderPagedView(Context context, AttributeSet attrs) {
super(context, attrs);
InvariantDeviceProfile profile = LauncherAppState.getIDP(context);
/* 修改最大行列 begin */
// mMaxCountX = profile.numFolderColumns;
// mMaxCountY = profile.numFolderRows;
mMaxCountX = 3;
mMaxCountY = 3;
/* 修改最大行列 0000009 end */
......
}
我这里就直接用固定数据代替了,其实应该用配置文件的。
再修改文件夹高度,我这里高度直接设置850:
src/com/android/launcher3/folder/Folder.java
private int getContentAreaHeight() {
/* 修改文件夹高度 begin */
// DeviceProfile grid = mLauncher.getDeviceProfile();
// int maxContentAreaHeight = grid.availableHeightPx
// - grid.getTotalWorkspacePadding().y - mFooterHeight;
// int height = Math.min(maxContentAreaHeight,
// mContent.getDesiredHeight());
// return Math.max(height, MIN_CONTENT_DIMEN);
return 850 - mFooterHeight;
/* 修改文件夹高度 end */
}
修改文件夹宽度
src/com/android/launcher3/folder/Folder.java
public int getDesiredWidth() {
/* 修改文件夹宽度 begin */
// return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
return getPaddingLeft() + getPaddingRight() + (3 * mCellWidth);
/* 修改文件夹宽度 end */
}
运行一下,发现文件夹的大小是固定了,但是没有在屏幕中间显示,继续搞
2:让文件夹在屏幕中间显示
这一步我走了很多冤枉路,其实很简单,几行代码就搞定
src/com/android/launcher3/folder/Folder.java -> centerAboutIcon():
private void centerAboutIcon() {
......
} else {
// Folder height is less than page height, so bound it to the absolute open folder
// bounds if necessary
Rect folderBounds = grid.getAbsoluteOpenFolderBounds();
left = Math.max(folderBounds.left, Math.min(left, folderBounds.right - width));
top = Math.max(folderBounds.top, Math.min(top, folderBounds.bottom - height));
}
int folderPivotX = width / 2 + (centeredLeft - left);
int folderPivotY = height / 2 + (centeredTop - top);
setPivotX(folderPivotX);
setPivotY(folderPivotY);
mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
(1.0f * folderPivotX / width));
mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
(1.0f * folderPivotY / height));
lp.width = width;
lp.height = height;
/* 修改left top begin */
// lp.x = left;
// lp.y = top;
lp.x = (850 - getFolderWidth()) / 2;
lp.y = 450;
/* 修改left top */
}
运行一下,文件夹可以在屏幕中间显示,但是有一个问题,当文件夹中只有两个app的时候,不是靠左显示,而是居中显示的
3:文件夹内部APP靠左展示
本来以为这个应该是LayoutParams属性控制的,就去找对应的属性,但是始终没有效果;偶然跟进方法的时候,进入到
src/com/android/launcher3/CellLayout.java -> onLayout():
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
int left = getPaddingLeft();
/* 靠左展示 begin */
// if (!isFullscreen) {
// left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
// }
/* 靠左展示 end */
int right = r - l - getPaddingRight();
if (!isFullscreen) {
right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
}
int top = getPaddingTop();
int bottom = b - t - getPaddingBottom();
mTouchFeedbackView.layout(left, top,
left + mTouchFeedbackView.getMeasuredWidth(),
top + mTouchFeedbackView.getMeasuredHeight());
mShortcutsAndWidgets.layout(left, top, right, bottom);
// Expand the background drawing bounds by the padding baked into the background drawable
mBackground.getPadding(mTempRect);
mBackground.setBounds(
left - mTempRect.left - getPaddingLeft(),
top - mTempRect.top - getPaddingTop(),
right + mTempRect.right + getPaddingRight(),
bottom + mTempRect.bottom + getPaddingBottom());
}
看到 isFullscreen 的时候,有一种解脱的感觉(终于找到地方了),顺利解决这个问题。
但是这个时候如果文件夹内有4个app,是2*2摆放的,所以再修改摆放位置:
src/com/android/launcher3/folder/FolderPagedView.java -> calculateGridSize():
public static void calculateGridSize(int count, int countX, int countY, int maxCountX,
int maxCountY, int maxItemsPerPage, int[] out) {
boolean done;
int gridCountX = countX;
int gridCountY = countY;
if (count >= maxItemsPerPage) {
gridCountX = maxCountX;
gridCountY = maxCountY;
done = true;
} else {
done = false;
/* 添加新计算 begin */
if(count >3){
gridCountX = 3;
gridCountY = (count+1)/4 + 1;
}else{
gridCountX = count;
gridCountY =1 ;
}
/* 添加新计算 end */
}
/* 注释旧计算 begin */
// while (!done) {
// int oldCountX = gridCountX;
// int oldCountY = gridCountY;
// if (gridCountX * gridCountY < count) {
// // Current grid is too small, expand it
// if ((gridCountX <= gridCountY || gridCountY == maxCountY)
// && gridCountX < maxCountX) {
// gridCountX++;
// } else if (gridCountY < maxCountY) {
// gridCountY++;
// }
// if (gridCountY == 0) gridCountY++;
// } else if ((gridCountY - 1) * gridCountX >= count && gridCountY >= gridCountX) {
// gridCountY = Math.max(0, gridCountY - 1);
// } else if ((gridCountX - 1) * gridCountY >= count) {
// gridCountX = Math.max(0, gridCountX - 1);
// }
// done = gridCountX == oldCountX && gridCountY == oldCountY;
// }
/* 注释旧计算 end */
out[0] = gridCountX;
out[1] = gridCountY;
}
4:文件夹展开的时候,隐藏workspace和HotSeat
不多说,上代码
src/com/android/launcher3/folder/Folder.java -> animateOpen():
public void animateOpen() {
......
sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
dragLayer.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
/* 隐藏workspace和HotSeat begin */
mLauncher.getWorkspace().setVisibility(View.GONE);
mLauncher.getHotseat().setVisibility(View.GONE);
/* 隐藏workspace和HotSeat 0000009 end */
}
src/com/android/launcher3/folder/Folder.java -> handleClose():
@Override
protected void handleClose(boolean animate) {
mIsOpen = false;
if (isEditingName()) {
mFolderName.dispatchBackKey();
}
if (mFolderIcon != null) {
if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION) {
mFolderIcon.clearLeaveBehindIfExists();
} else {
mFolderIcon.shrinkAndFadeIn(animate);
}
}
if (!(getParent() instanceof DragLayer)) return;
DragLayer parent = (DragLayer) getParent();
if (animate) {
animateClosed();
} else {
closeComplete(false);
}
// Notify the accessibility manager that this folder "window" has disappeared and no
// longer occludes the workspace items
parent.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
/* 显示workspace和HotSeat begin */
mLauncher.getWorkspace().setVisibility(View.VISIBLE);
mLauncher.getHotseat().setVisibility(View.VISIBLE);
/* 显示workspace和HotSeat end */
}
总结:这几天一直在看launcher,都快看吐了,得缓几天,先看看别的模块(SystemUI,Settings,Gallery)