AnimateableViewBounds.java revision dcfa7976fa836ae90bb4a579892a18a0abf35b3c
1/* 2 * Copyright (C) 2014 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 17package com.android.systemui.recents.views; 18 19import android.animation.ObjectAnimator; 20import android.graphics.Outline; 21import android.graphics.Rect; 22import android.view.View; 23import android.view.ViewOutlineProvider; 24import com.android.systemui.recents.RecentsConfiguration; 25 26/* An outline provider that has a clip and outline that can be animated. */ 27public class AnimateableViewBounds extends ViewOutlineProvider { 28 29 RecentsConfiguration mConfig; 30 31 View mSourceView; 32 Rect mClipRect = new Rect(); 33 Rect mOutlineClipRect = new Rect(); 34 int mCornerRadius; 35 36 ObjectAnimator mClipTopAnimator; 37 ObjectAnimator mClipBottomAnimator; 38 39 public AnimateableViewBounds(View source, int cornerRadius) { 40 mConfig = RecentsConfiguration.getInstance(); 41 mSourceView = source; 42 mCornerRadius = cornerRadius; 43 mSourceView.setClipToOutline(true); 44 setClipTop(getClipTop()); 45 setClipBottom(getClipBottom()); 46 setOutlineClipBottom(getOutlineClipBottom()); 47 } 48 49 @Override 50 public void getOutline(View view, Outline outline) { 51 outline.setRoundRect(Math.max(mClipRect.left, mOutlineClipRect.left), 52 Math.max(mClipRect.top, mOutlineClipRect.top), 53 mSourceView.getMeasuredWidth() - Math.max(mClipRect.right, mOutlineClipRect.right), 54 mSourceView.getMeasuredHeight() - Math.max(mClipRect.bottom, mOutlineClipRect.bottom), 55 mCornerRadius); 56 } 57 58 /** Animates the top clip. */ 59 void animateClipTop(int top, int duration) { 60 if (mClipTopAnimator != null) { 61 mClipTopAnimator.removeAllListeners(); 62 mClipTopAnimator.cancel(); 63 } 64 mClipTopAnimator = ObjectAnimator.ofInt(this, "clipTop", top); 65 mClipTopAnimator.setDuration(duration); 66 mClipTopAnimator.setInterpolator(mConfig.fastOutSlowInInterpolator); 67 mClipTopAnimator.start(); 68 } 69 70 /** Sets the top clip. */ 71 public void setClipTop(int top) { 72 if (top != mClipRect.top) { 73 mClipRect.top = top; 74 mSourceView.invalidateOutline(); 75 } 76 } 77 78 /** Returns the top clip. */ 79 public int getClipTop() { 80 return mClipRect.top; 81 } 82 83 /** Animates the bottom clip. */ 84 void animateClipBottom(int bottom, int duration) { 85 if (mClipTopAnimator != null) { 86 mClipTopAnimator.removeAllListeners(); 87 mClipTopAnimator.cancel(); 88 } 89 mClipTopAnimator = ObjectAnimator.ofInt(this, "clipBottom", bottom); 90 mClipTopAnimator.setDuration(duration); 91 mClipTopAnimator.setInterpolator(mConfig.fastOutSlowInInterpolator); 92 mClipTopAnimator.start(); 93 } 94 95 /** Sets the bottom clip. */ 96 public void setClipBottom(int bottom) { 97 if (bottom != mClipRect.bottom) { 98 mClipRect.bottom = bottom; 99 mSourceView.invalidateOutline(); 100 } 101 } 102 103 /** Returns the bottom clip. */ 104 public int getClipBottom() { 105 return mClipRect.bottom; 106 } 107 108 public void setOutlineClipBottom(int bottom) { 109 if (bottom != mOutlineClipRect.bottom) { 110 mOutlineClipRect.bottom = bottom; 111 mSourceView.invalidateOutline(); 112 } 113 } 114 115 public int getOutlineClipBottom() { 116 return mOutlineClipRect.bottom; 117 } 118} 119