Home | History | Annotate | Download | only in folder
      1 package com.android.launcher3.folder;
      2 
      3 import android.graphics.Path;
      4 import android.graphics.Point;
      5 
      6 import com.android.launcher3.DeviceProfile;
      7 import com.android.launcher3.LauncherAppState;
      8 import com.android.launcher3.Utilities;
      9 
     10 public class ClippedFolderIconLayoutRule implements FolderIcon.PreviewLayoutRule {
     11 
     12     static final int MAX_NUM_ITEMS_IN_PREVIEW = 4;
     13     private static final int MIN_NUM_ITEMS_IN_PREVIEW = 2;
     14 
     15     final float MIN_SCALE = 0.48f;
     16     final float MAX_SCALE = 0.58f;
     17     final float MAX_RADIUS_DILATION = 0.15f;
     18     final float ITEM_RADIUS_SCALE_FACTOR = 1.33f;
     19 
     20     private float[] mTmpPoint = new float[2];
     21 
     22     private float mAvailableSpace;
     23     private float mRadius;
     24     private float mIconSize;
     25     private boolean mIsRtl;
     26     private float mBaselineIconScale;
     27 
     28     @Override
     29     public void init(int availableSpace, int intrinsicIconSize, boolean rtl) {
     30         mAvailableSpace = availableSpace;
     31         mRadius = ITEM_RADIUS_SCALE_FACTOR * availableSpace / 2f;
     32         mIconSize = intrinsicIconSize;
     33         mIsRtl = rtl;
     34         mBaselineIconScale = availableSpace / (intrinsicIconSize * 1f);
     35     }
     36 
     37     @Override
     38     public FolderIcon.PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
     39             int curNumItems, FolderIcon.PreviewItemDrawingParams params) {
     40 
     41         float totalScale = scaleForNumItems(curNumItems);
     42         float transX;
     43         float transY;
     44         float overlayAlpha = 0;
     45 
     46         // Items beyond those displayed in the preview are animated to the center
     47         if (index >= MAX_NUM_ITEMS_IN_PREVIEW) {
     48             transX = transY = mAvailableSpace / 2 - (mIconSize * totalScale) / 2;
     49         } else {
     50             getPosition(index, curNumItems, mTmpPoint);
     51             transX = mTmpPoint[0];
     52             transY = mTmpPoint[1];
     53         }
     54 
     55         if (params == null) {
     56             params = new FolderIcon.PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
     57         } else {
     58             params.update(transX, transY, totalScale);
     59             params.overlayAlpha = overlayAlpha;
     60         }
     61         return params;
     62     }
     63 
     64     private void getPosition(int index, int curNumItems, float[] result) {
     65         // The case of two items is homomorphic to the case of one.
     66         curNumItems = Math.max(curNumItems, 2);
     67 
     68         // We model the preview as a circle of items starting in the appropriate piece of the
     69         // upper left quadrant (to achieve horizontal and vertical symmetry).
     70         double theta0 = mIsRtl ? 0 : Math.PI;
     71 
     72         // In RTL we go counterclockwise
     73         int direction = mIsRtl ? 1 : -1;
     74 
     75         double thetaShift = 0;
     76         if (curNumItems == 3) {
     77             thetaShift = Math.PI / 6;
     78         } else if (curNumItems == 4) {
     79             thetaShift = Math.PI / 4;
     80         }
     81         theta0 += direction * thetaShift;
     82 
     83         // We want the items to appear in reading order. For the case of 1, 2 and 3 items, this
     84         // is natural for the circular model. With 4 items, however, we need to swap the 3rd and
     85         // 4th indices to achieve reading order.
     86         if (curNumItems == 4 && index == 3) {
     87             index = 2;
     88         } else if (curNumItems == 4 && index == 2) {
     89             index = 3;
     90         }
     91 
     92         // We bump the radius up between 0 and MAX_RADIUS_DILATION % as the number of items increase
     93         float radius = mRadius * (1 + MAX_RADIUS_DILATION * (curNumItems -
     94                 MIN_NUM_ITEMS_IN_PREVIEW) / (MAX_NUM_ITEMS_IN_PREVIEW - MIN_NUM_ITEMS_IN_PREVIEW));
     95         double theta = theta0 + index * (2 * Math.PI / curNumItems) * direction;
     96 
     97         float halfIconSize = (mIconSize * scaleForNumItems(curNumItems)) / 2;
     98 
     99         // Map the location along the circle, and offset the coordinates to represent the center
    100         // of the icon, and to be based from the top / left of the preview area. The y component
    101         // is inverted to match the coordinate system.
    102         result[0] = mAvailableSpace / 2 + (float) (radius * Math.cos(theta) / 2) - halfIconSize;
    103         result[1] = mAvailableSpace / 2 + (float) (- radius * Math.sin(theta) / 2) - halfIconSize;
    104 
    105     }
    106 
    107     private float scaleForNumItems(int numItems) {
    108         float scale = 1f;
    109         if (numItems <= 2) {
    110             scale = MAX_SCALE;
    111         } else if (numItems == 3) {
    112             scale = (MAX_SCALE + MIN_SCALE) / 2;
    113         } else {
    114             scale = MIN_SCALE;
    115         }
    116 
    117         return scale * mBaselineIconScale;
    118     }
    119 
    120     @Override
    121     public int numItems() {
    122         return MAX_NUM_ITEMS_IN_PREVIEW;
    123     }
    124 
    125     @Override
    126     public boolean clipToBackground() {
    127         return true;
    128     }
    129 
    130 }
    131