2016-02-05 14:47:50 -08:00
|
|
|
package com.android.launcher3.folder;
|
|
|
|
|
|
2017-09-08 11:59:05 -07:00
|
|
|
public class ClippedFolderIconLayoutRule {
|
2017-07-13 17:54:54 -07:00
|
|
|
|
2017-09-08 11:59:05 -07:00
|
|
|
public static final int MAX_NUM_ITEMS_IN_PREVIEW = 4;
|
2016-02-05 14:47:50 -08:00
|
|
|
private static final int MIN_NUM_ITEMS_IN_PREVIEW = 2;
|
|
|
|
|
|
2021-06-04 17:31:02 -07:00
|
|
|
private static final float MIN_SCALE = 0.44f;
|
2021-06-08 15:35:33 -07:00
|
|
|
private static final float MAX_SCALE = 0.51f;
|
2021-07-20 13:57:16 -07:00
|
|
|
private static final float MAX_RADIUS_DILATION = 0.25f;
|
|
|
|
|
// The max amount of overlap the preview items can go outside of the background bounds.
|
|
|
|
|
public static final float ICON_OVERLAP_FACTOR = 1 + (MAX_RADIUS_DILATION / 2f);
|
2021-06-08 15:35:33 -07:00
|
|
|
private static final float ITEM_RADIUS_SCALE_FACTOR = 1.15f;
|
2017-07-13 17:54:54 -07:00
|
|
|
|
2017-09-08 11:59:05 -07:00
|
|
|
public static final int EXIT_INDEX = -2;
|
|
|
|
|
public static final int ENTER_INDEX = -3;
|
2016-02-05 14:47:50 -08:00
|
|
|
|
|
|
|
|
private float[] mTmpPoint = new float[2];
|
|
|
|
|
|
|
|
|
|
private float mAvailableSpace;
|
|
|
|
|
private float mRadius;
|
|
|
|
|
private float mIconSize;
|
|
|
|
|
private boolean mIsRtl;
|
2016-03-21 14:33:02 -07:00
|
|
|
private float mBaselineIconScale;
|
2016-02-05 14:47:50 -08:00
|
|
|
|
2017-06-22 09:53:59 -07:00
|
|
|
public void init(int availableSpace, float intrinsicIconSize, boolean rtl) {
|
2016-02-05 14:47:50 -08:00
|
|
|
mAvailableSpace = availableSpace;
|
2016-02-24 19:19:06 -08:00
|
|
|
mRadius = ITEM_RADIUS_SCALE_FACTOR * availableSpace / 2f;
|
2016-02-05 14:47:50 -08:00
|
|
|
mIconSize = intrinsicIconSize;
|
|
|
|
|
mIsRtl = rtl;
|
2016-03-21 14:33:02 -07:00
|
|
|
mBaselineIconScale = availableSpace / (intrinsicIconSize * 1f);
|
2016-02-05 14:47:50 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-23 18:40:31 -07:00
|
|
|
public PreviewItemDrawingParams computePreviewItemDrawingParams(int index, int curNumItems,
|
|
|
|
|
PreviewItemDrawingParams params) {
|
2017-09-08 11:59:05 -07:00
|
|
|
float totalScale = scaleForItem(curNumItems);
|
2016-02-13 16:06:05 -08:00
|
|
|
float transX;
|
|
|
|
|
float transY;
|
2016-02-05 14:47:50 -08:00
|
|
|
|
2017-09-08 11:59:05 -07:00
|
|
|
if (index == EXIT_INDEX) {
|
2017-07-13 17:54:54 -07:00
|
|
|
// 0 1 * <-- Exit position (row 0, col 2)
|
|
|
|
|
// 2 3
|
|
|
|
|
getGridPosition(0, 2, mTmpPoint);
|
2017-09-08 11:59:05 -07:00
|
|
|
} else if (index == ENTER_INDEX) {
|
2017-07-13 17:54:54 -07:00
|
|
|
// 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;
|
2016-02-13 16:06:05 -08:00
|
|
|
} else {
|
|
|
|
|
getPosition(index, curNumItems, mTmpPoint);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 17:54:54 -07:00
|
|
|
transX = mTmpPoint[0];
|
|
|
|
|
transY = mTmpPoint[1];
|
|
|
|
|
|
2016-02-05 14:47:50 -08:00
|
|
|
if (params == null) {
|
2021-06-15 12:55:20 -07:00
|
|
|
params = new PreviewItemDrawingParams(transX, transY, totalScale);
|
2016-02-05 14:47:50 -08:00
|
|
|
} else {
|
2016-02-13 16:06:05 -08:00
|
|
|
params.update(transX, transY, totalScale);
|
2016-02-05 14:47:50 -08:00
|
|
|
}
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 17:54:54 -07:00
|
|
|
/**
|
|
|
|
|
* Builds a grid based on the positioning of the items when there are
|
|
|
|
|
* {@link #MAX_NUM_ITEMS_IN_PREVIEW} in the preview.
|
|
|
|
|
*
|
|
|
|
|
* Positions in the grid: 0 1 // 0 is row 0, col 1
|
|
|
|
|
* 2 3 // 3 is row 1, col 1
|
|
|
|
|
*/
|
|
|
|
|
private void getGridPosition(int row, int col, float[] result) {
|
|
|
|
|
// We use position 0 and 3 to calculate the x and y distances between items.
|
|
|
|
|
getPosition(0, 4, result);
|
|
|
|
|
float left = result[0];
|
|
|
|
|
float top = result[1];
|
|
|
|
|
|
|
|
|
|
getPosition(3, 4, result);
|
|
|
|
|
float dx = result[0] - left;
|
|
|
|
|
float dy = result[1] - top;
|
|
|
|
|
|
|
|
|
|
result[0] = left + (col * dx);
|
|
|
|
|
result[1] = top + (row * dy);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 14:47:50 -08:00
|
|
|
private void getPosition(int index, int curNumItems, float[] result) {
|
|
|
|
|
// The case of two items is homomorphic to the case of one.
|
|
|
|
|
curNumItems = Math.max(curNumItems, 2);
|
|
|
|
|
|
|
|
|
|
// We model the preview as a circle of items starting in the appropriate piece of the
|
|
|
|
|
// upper left quadrant (to achieve horizontal and vertical symmetry).
|
|
|
|
|
double theta0 = mIsRtl ? 0 : Math.PI;
|
|
|
|
|
|
|
|
|
|
// In RTL we go counterclockwise
|
|
|
|
|
int direction = mIsRtl ? 1 : -1;
|
|
|
|
|
|
|
|
|
|
double thetaShift = 0;
|
|
|
|
|
if (curNumItems == 3) {
|
2021-03-19 12:07:19 -04:00
|
|
|
thetaShift = Math.PI / 2;
|
2016-02-05 14:47:50 -08:00
|
|
|
} else if (curNumItems == 4) {
|
|
|
|
|
thetaShift = Math.PI / 4;
|
|
|
|
|
}
|
|
|
|
|
theta0 += direction * thetaShift;
|
|
|
|
|
|
|
|
|
|
// We want the items to appear in reading order. For the case of 1, 2 and 3 items, this
|
|
|
|
|
// is natural for the circular model. With 4 items, however, we need to swap the 3rd and
|
|
|
|
|
// 4th indices to achieve reading order.
|
|
|
|
|
if (curNumItems == 4 && index == 3) {
|
|
|
|
|
index = 2;
|
|
|
|
|
} else if (curNumItems == 4 && index == 2) {
|
|
|
|
|
index = 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We bump the radius up between 0 and MAX_RADIUS_DILATION % as the number of items increase
|
|
|
|
|
float radius = mRadius * (1 + MAX_RADIUS_DILATION * (curNumItems -
|
|
|
|
|
MIN_NUM_ITEMS_IN_PREVIEW) / (MAX_NUM_ITEMS_IN_PREVIEW - MIN_NUM_ITEMS_IN_PREVIEW));
|
|
|
|
|
double theta = theta0 + index * (2 * Math.PI / curNumItems) * direction;
|
|
|
|
|
|
2017-09-08 11:59:05 -07:00
|
|
|
float halfIconSize = (mIconSize * scaleForItem(curNumItems)) / 2;
|
2016-02-05 14:47:50 -08:00
|
|
|
|
|
|
|
|
// Map the location along the circle, and offset the coordinates to represent the center
|
|
|
|
|
// of the icon, and to be based from the top / left of the preview area. The y component
|
|
|
|
|
// is inverted to match the coordinate system.
|
|
|
|
|
result[0] = mAvailableSpace / 2 + (float) (radius * Math.cos(theta) / 2) - halfIconSize;
|
|
|
|
|
result[1] = mAvailableSpace / 2 + (float) (- radius * Math.sin(theta) / 2) - halfIconSize;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-08 11:59:05 -07:00
|
|
|
public float scaleForItem(int numItems) {
|
2017-02-06 15:45:53 -08:00
|
|
|
// Scale is determined by the number of items in the preview.
|
2017-09-08 11:59:05 -07:00
|
|
|
final float scale;
|
2021-06-04 17:31:02 -07:00
|
|
|
if (numItems <= 3) {
|
2016-03-21 14:33:02 -07:00
|
|
|
scale = MAX_SCALE;
|
2016-02-05 14:47:50 -08:00
|
|
|
} else {
|
2016-03-21 14:33:02 -07:00
|
|
|
scale = MIN_SCALE;
|
2016-02-05 14:47:50 -08:00
|
|
|
}
|
2016-03-21 14:33:02 -07:00
|
|
|
return scale * mBaselineIconScale;
|
2016-02-05 14:47:50 -08:00
|
|
|
}
|
|
|
|
|
|
2017-03-01 16:06:21 -08:00
|
|
|
public float getIconSize() {
|
|
|
|
|
return mIconSize;
|
|
|
|
|
}
|
2016-02-05 14:47:50 -08:00
|
|
|
}
|