未修改前效果
ClippedFolderIconLayoutRule
// public static final int MAX_NUM_ITEMS_IN_PREVIEW = 4;
//文件夹打开可以显示的最大数目,该成16
public static final int MAX_NUM_ITEMS_IN_PREVIEW = 16;
computePreviewItemDrawingParams()方法默认是2排2列的效果,现在改成4排4列的
private float mTotalScal;
public PreviewItemDrawingParams computePreviewItemDrawingParams(int index, int curNumItems,
PreviewItemDrawingParams params) {
float totalScale = scaleForItem(curNumItems);
float transX;
float transY;
// if (index == EXIT_INDEX) {
// // 0 1 * <-- Exit position (row 0, col 2)
// // 2 3
// getGridPosition(0, 2, mTmpPoint);
// } else if (index == ENTER_INDEX) {
// // 0 1
// // 2 3 * <-- Enter position (row 1, col 2)
// getGridPosition(1, 2, mTmpPoint);
// } else if (index >= MAX_NUM_ITEMS_IN_PREVIEW) {
// // Items beyond those displayed in the preview are animated to the center
// mTmpPoint[0] = mTmpPoint[1] = mAvailableSpace / 2 - (mIconSize * totalScale) / 2;
// } else {
// getPosition(index, curNumItems, mTmpPoint);
// }
//
// transX = mTmpPoint[0];
// transY = mTmpPoint[1];
//
// if (params == null) {
// params = new PreviewItemDrawingParams(transX, transY, totalScale);
// } else {
// params.update(transX, transY, totalScale);
// }
// return params;
//上面原来的逻辑注释掉
//计算某个图标在文件夹中的位置
getCusPosition(index, mTmpPoint);
transX = mTmpPoint[0];
transY = mTmpPoint[1];
if (params == null) {
params = new PreviewItemDrawingParams(transX, transY, mTotalScal);
} else {
params.update(transX, transY, mTotalScal);
}
return params;
}
private void getCusPosition(int index, float[] result) {
int sqrt = 4;
//第几行
int rowNum = index%4;
//第几列
int columnNum = index/4;
//图标之间的间距
float padding = 2;
float iconSize = (mAvailableSpace - (sqrt+1)*padding)/sqrt;
mTotalScal = iconSize/mIconSize;
//x方向的坐标值
result[0] = (iconSize + padding/2)*rowNum + padding;
//y方向的坐标值
result[1] = (iconSize + padding/2)*columnNum + padding;
}
FolderGridOrganizer
public FolderGridOrganizer(InvariantDeviceProfile profile) {
// mMaxCountX = profile.numFolderColumns;
// mMaxCountY = profile.numFolderRows;
//改成4*4方式
mMaxCountX = 4;
mMaxCountY = 4;
mMaxItemsPerPage = mMaxCountX * mMaxCountY;
}
calculateGridSize()方法
private void calculateGridSize(int count) {
boolean done;
int gridCountX = mCountX;
int gridCountY = mCountY;
if (count >= mMaxItemsPerPage) {
gridCountX = mMaxCountX;
gridCountY = mMaxCountY;
done = true;
} else {
done = false;
}
while (!done) {
int oldCountX = gridCountX;
int oldCountY = gridCountY;
if (gridCountX * gridCountY < count) {
// Current grid is too small, expand it
if ((gridCountX <= gridCountY || gridCountY == mMaxCountY)
&& gridCountX < mMaxCountX) {
gridCountX++;
} else if (gridCountY < mMaxCountY) {
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;
}
// mCountX = gridCountX;
// mCountY = gridCountY;
//添加多这个判断
if(count <= 9){
mCountX = 4;
mCountY = count/4 + (count%4==0?0:1);
}else {
mCountX = gridCountX;
mCountY = gridCountY;
}
}
isItemInPreview()方法注释掉一些内容
public boolean isItemInPreview(int page, int rank) {
// First page items are laid out such that the first 4 items are always in the upper
// left quadrant. For all other pages, we need to check the row and col.
//4*4布局注释掉
// if (page > 0 || mDisplayingUpperLeftQuadrant) {
// int col = rank % mCountX;
// int row = rank / mCountX;
// return col < 2 && row < 2;
// }
return rank < MAX_NUM_ITEMS_IN_PREVIEW;
}
效果