yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.newbubble; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ValueAnimator; |
| 22 | import android.graphics.Outline; |
| 23 | import android.graphics.Rect; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewOutlineProvider; |
| 26 | |
| 27 | /** |
| 28 | * A {@link ViewOutlineProvider} that provides an outline that interpolates between two radii and |
| 29 | * two {@link Rect}s. |
| 30 | * |
| 31 | * <p>An example usage of this provider is an outline that starts out as a circle and ends as a |
| 32 | * rounded rectangle. |
| 33 | */ |
| 34 | public class RoundedRectRevealOutlineProvider extends ViewOutlineProvider { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 35 | private final float startRadius; |
| 36 | private final float endRadius; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 37 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 38 | private final Rect startRect; |
| 39 | private final Rect endRect; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 40 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 41 | private final Rect outline; |
| 42 | private float outlineRadius; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 43 | |
| 44 | public RoundedRectRevealOutlineProvider( |
| 45 | float startRadius, float endRadius, Rect startRect, Rect endRect) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 46 | this.startRadius = startRadius; |
| 47 | this.endRadius = endRadius; |
| 48 | this.startRect = startRect; |
| 49 | this.endRect = endRect; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 50 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 51 | outline = new Rect(); |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void getOutline(View v, Outline outline) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 56 | outline.setRoundRect(this.outline, outlineRadius); |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** Sets the progress, from 0 to 1, of the reveal animation. */ |
| 60 | public void setProgress(float progress) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 61 | outlineRadius = (1 - progress) * startRadius + progress * endRadius; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 62 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 63 | outline.left = (int) ((1 - progress) * startRect.left + progress * endRect.left); |
| 64 | outline.top = (int) ((1 - progress) * startRect.top + progress * endRect.top); |
| 65 | outline.right = (int) ((1 - progress) * startRect.right + progress * endRect.right); |
| 66 | outline.bottom = (int) ((1 - progress) * startRect.bottom + progress * endRect.bottom); |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ValueAnimator createRevealAnimator(final View revealView, boolean isReversed) { |
| 70 | ValueAnimator valueAnimator = |
| 71 | isReversed ? ValueAnimator.ofFloat(1f, 0f) : ValueAnimator.ofFloat(0f, 1f); |
| 72 | |
| 73 | valueAnimator.addListener( |
| 74 | new AnimatorListenerAdapter() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 75 | private boolean wasCanceled = false; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void onAnimationStart(Animator animation) { |
| 79 | revealView.setOutlineProvider(RoundedRectRevealOutlineProvider.this); |
| 80 | revealView.setClipToOutline(true); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onAnimationCancel(Animator animation) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 85 | wasCanceled = true; |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onAnimationEnd(Animator animation) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 90 | if (!wasCanceled) { |
yueg | 0b4755c | 2017-12-18 10:01:03 -0800 | [diff] [blame] | 91 | revealView.setOutlineProvider(ViewOutlineProvider.BACKGROUND); |
| 92 | revealView.setClipToOutline(false); |
| 93 | } |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | valueAnimator.addUpdateListener( |
| 98 | (currentValueAnimator) -> { |
| 99 | float progress = (Float) currentValueAnimator.getAnimatedValue(); |
| 100 | setProgress(progress); |
| 101 | revealView.invalidateOutline(); |
| 102 | }); |
| 103 | return valueAnimator; |
| 104 | } |
| 105 | } |