1 /** 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.folder; 18 19 public class StackFolderIconLayoutRule implements FolderIcon.PreviewLayoutRule { 20 21 static final int MAX_NUM_ITEMS_IN_PREVIEW = 3; 22 23 // The degree to which the item in the back of the stack is scaled [0...1] 24 // (0 means it's not scaled at all, 1 means it's scaled to nothing) 25 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f; 26 27 // The amount of vertical spread between items in the stack [0...1] 28 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.18f; 29 30 private float mBaselineIconScale; 31 private int mBaselineIconSize; 32 private int mAvailableSpaceInPreview; 33 private float mMaxPerspectiveShift; 34 35 @Override 36 public void init(int availableSpace, float intrinsicIconSize, boolean rtl) { 37 mAvailableSpaceInPreview = availableSpace; 38 39 // cos(45) = 0.707 + ~= 0.1) = 0.8f 40 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f)); 41 42 int unscaledHeight = (int) (intrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR)); 43 44 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight); 45 46 mBaselineIconSize = (int) (intrinsicIconSize * mBaselineIconScale); 47 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR; 48 } 49 50 @Override 51 public PreviewItemDrawingParams computePreviewItemDrawingParams(int index, int curNumItems, 52 PreviewItemDrawingParams params) { 53 float scale = scaleForItem(index, curNumItems); 54 55 index = MAX_NUM_ITEMS_IN_PREVIEW - index - 1; 56 float r = (index * 1.0f) / (MAX_NUM_ITEMS_IN_PREVIEW - 1); 57 58 float offset = (1 - r) * mMaxPerspectiveShift; 59 float scaledSize = scale * mBaselineIconSize; 60 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize; 61 62 // We want to imagine our coordinates from the bottom left, growing up and to the 63 // right. This is natural for the x-axis, but for the y-axis, we have to invert things. 64 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection); 65 float transX = (mAvailableSpaceInPreview - scaledSize) / 2; 66 float totalScale = mBaselineIconScale * scale; 67 final float overlayAlpha = (80 * (1 - r)) / 255f; 68 69 if (params == null) { 70 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha); 71 } else { 72 params.update(transX, transY, totalScale); 73 params.overlayAlpha = overlayAlpha; 74 } 75 return params; 76 } 77 78 @Override 79 public int maxNumItems() { 80 return MAX_NUM_ITEMS_IN_PREVIEW; 81 } 82 83 @Override 84 public float getIconSize() { 85 return mBaselineIconSize; 86 } 87 88 @Override 89 public float scaleForItem(int index, int numItems) { 90 // Scale is determined by the position of the icon in the preview. 91 index = MAX_NUM_ITEMS_IN_PREVIEW - index - 1; 92 float r = (index * 1.0f) / (MAX_NUM_ITEMS_IN_PREVIEW - 1); 93 return (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r)); 94 } 95 96 @Override 97 public boolean clipToBackground() { 98 return false; 99 } 100 101 @Override 102 public boolean hasEnterExitIndices() { 103 return false; 104 } 105 106 @Override 107 public int getExitIndex() { 108 throw new RuntimeException("hasEnterExitIndices not supported"); 109 } 110 111 @Override 112 public int getEnterIndex() { 113 throw new RuntimeException("hasEnterExitIndices not supported"); 114 } 115 } 116