1 /* 2 * Copyright (C) 2016 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.util; 18 19 import android.graphics.Rect; 20 import android.view.ViewOutlineProvider; 21 22 /** 23 * A {@link ViewOutlineProvider} that animates a reveal in a "pill" shape. 24 * A pill is simply a round rect, but we assume the width is greater than 25 * the height and that the radius is equal to half the height. 26 */ 27 public class PillRevealOutlineProvider extends RevealOutlineAnimation { 28 29 private int mCenterX; 30 private int mCenterY; 31 private float mFinalRadius; 32 protected Rect mPillRect; 33 34 /** 35 * @param x reveal center x 36 * @param y reveal center y 37 * @param pillRect round rect that represents the final pill shape 38 */ 39 public PillRevealOutlineProvider(int x, int y, Rect pillRect) { 40 this(x, y, pillRect, pillRect.height() / 2f); 41 } 42 43 public PillRevealOutlineProvider(int x, int y, Rect pillRect, float radius) { 44 mCenterX = x; 45 mCenterY = y; 46 mPillRect = pillRect; 47 mOutlineRadius = mFinalRadius = radius; 48 } 49 50 @Override 51 public boolean shouldRemoveElevationDuringAnimation() { 52 return false; 53 } 54 55 @Override 56 public void setProgress(float progress) { 57 // Assumes width is greater than height. 58 int centerToEdge = Math.max(mCenterX, mPillRect.width() - mCenterX); 59 int currentSize = (int) (progress * centerToEdge); 60 61 // Bound the outline to the final pill shape defined by mPillRect. 62 mOutline.left = Math.max(mPillRect.left, mCenterX - currentSize); 63 mOutline.top = Math.max(mPillRect.top, mCenterY - currentSize); 64 mOutline.right = Math.min(mPillRect.right, mCenterX + currentSize); 65 mOutline.bottom = Math.min(mPillRect.bottom, mCenterY + currentSize); 66 mOutlineRadius = Math.min(mFinalRadius, mOutline.height() / 2); 67 } 68 } 69