QSDetailClipper.java revision 8af525dd7d1640175fda344301a8712725557caa
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.qs; 18 19import android.animation.Animator; 20import android.animation.Animator.AnimatorListener; 21import android.animation.AnimatorListenerAdapter; 22import android.graphics.drawable.TransitionDrawable; 23import android.view.View; 24import android.view.ViewAnimationUtils; 25 26/** Helper for quick settings detail panel clip animations. **/ 27public class QSDetailClipper { 28 29 private final View mDetail; 30 private final TransitionDrawable mBackground; 31 32 private Animator mAnimator; 33 34 public QSDetailClipper(View detail) { 35 mDetail = detail; 36 mBackground = (TransitionDrawable) detail.getBackground(); 37 } 38 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 mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, 0, r); 50 mAnimator.setDuration((long)(mAnimator.getDuration() * 1.5)); 51 if (listener != null) { 52 mAnimator.addListener(listener); 53 } 54 if (in) { 55 mBackground.startTransition((int)(mAnimator.getDuration() * 0.6)); 56 mAnimator.addListener(mVisibleOnStart); 57 mAnimator.start(); 58 } else { 59 mAnimator.addListener(mGoneOnEnd); 60 mAnimator.reverse(); 61 } 62 } 63 64 private final AnimatorListenerAdapter mVisibleOnStart = new AnimatorListenerAdapter() { 65 @Override 66 public void onAnimationStart(Animator animation) { 67 mDetail.setVisibility(View.VISIBLE); 68 } 69 70 public void onAnimationEnd(Animator animation) { 71 mAnimator = null; 72 } 73 }; 74 75 private final AnimatorListenerAdapter mGoneOnEnd = new AnimatorListenerAdapter() { 76 @Override 77 public void onAnimationEnd(Animator animation) { 78 mDetail.setVisibility(View.GONE); 79 mBackground.resetTransition(); 80 mAnimator = null; 81 }; 82 }; 83} 84