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 17 package com.android.systemui.qs; 18 19 import android.animation.Animator; 20 import android.animation.Animator.AnimatorListener; 21 import android.animation.AnimatorListenerAdapter; 22 import android.graphics.drawable.TransitionDrawable; 23 import android.view.View; 24 import android.view.ViewAnimationUtils; 25 26 /** Helper for quick settings detail panel clip animations. **/ 27 public class QSDetailClipper { 28 29 private final View mDetail; 30 private final TransitionDrawable mBackground; 31 32 private Animator mAnimator; 33 QSDetailClipper(View detail)34 public QSDetailClipper(View detail) { 35 mDetail = detail; 36 mBackground = (TransitionDrawable) detail.getBackground(); 37 } 38 animateCircularClip(int x, int y, boolean in, AnimatorListener listener)39 public void animateCircularClip(int x, int y, boolean in, AnimatorListener listener) { 40 if (mAnimator != null) { 41 mAnimator.cancel(); 42 } 43 final int w = mDetail.getWidth() - x; 44 final int h = mDetail.getHeight() - y; 45 int r = (int) Math.ceil(Math.sqrt(x * x + y * y)); 46 r = (int) Math.max(r, Math.ceil(Math.sqrt(w * w + y * y))); 47 r = (int) Math.max(r, Math.ceil(Math.sqrt(w * w + h * h))); 48 r = (int) Math.max(r, Math.ceil(Math.sqrt(x * x + h * h))); 49 if (in) { 50 mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, 0, r); 51 } else { 52 mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, r, 0); 53 } 54 mAnimator.setDuration((long)(mAnimator.getDuration() * 1.5)); 55 if (listener != null) { 56 mAnimator.addListener(listener); 57 } 58 if (in) { 59 mBackground.startTransition((int)(mAnimator.getDuration() * 0.6)); 60 mAnimator.addListener(mVisibleOnStart); 61 } else { 62 mDetail.postDelayed(mReverseBackground, (long)(mAnimator.getDuration() * 0.65)); 63 mAnimator.addListener(mGoneOnEnd); 64 } 65 mAnimator.start(); 66 } 67 68 private final Runnable mReverseBackground = new Runnable() { 69 @Override 70 public void run() { 71 if (mAnimator != null) { 72 mBackground.reverseTransition((int)(mAnimator.getDuration() * 0.35)); 73 } 74 } 75 }; 76 77 private final AnimatorListenerAdapter mVisibleOnStart = new AnimatorListenerAdapter() { 78 @Override 79 public void onAnimationStart(Animator animation) { 80 mDetail.setVisibility(View.VISIBLE); 81 } 82 83 public void onAnimationEnd(Animator animation) { 84 mAnimator = null; 85 } 86 }; 87 88 private final AnimatorListenerAdapter mGoneOnEnd = new AnimatorListenerAdapter() { 89 @Override 90 public void onAnimationEnd(Animator animation) { 91 mDetail.setVisibility(View.GONE); 92 mBackground.resetTransition(); 93 mAnimator = null; 94 }; 95 }; 96 } 97