QSDetailClipper.java revision 19328fab0e7d1509c4c2f9276900dbd87bce81ea
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        mDetail.setLayerType(View.LAYER_TYPE_HARDWARE, null);
55        if (in) {
56            mBackground.startTransition((int)(mAnimator.getDuration() * 0.6));
57            mAnimator.addListener(mVisibleOnStart);
58            mAnimator.start();
59        } else {
60            mDetail.postDelayed(mReverseBackground, (long)(mAnimator.getDuration() * 0.65));
61            mAnimator.addListener(mGoneOnEnd);
62            mAnimator.reverse();
63        }
64    }
65
66    private final Runnable mReverseBackground = new Runnable() {
67        @Override
68        public void run() {
69            if (mAnimator != null) {
70                mBackground.reverseTransition((int)(mAnimator.getDuration() * 0.35));
71            }
72        }
73    };
74
75    private final AnimatorListenerAdapter mVisibleOnStart = new AnimatorListenerAdapter() {
76        @Override
77        public void onAnimationStart(Animator animation) {
78            mDetail.setVisibility(View.VISIBLE);
79        }
80
81        public void onAnimationEnd(Animator animation) {
82            mDetail.setLayerType(View.LAYER_TYPE_NONE, null);
83            mAnimator = null;
84        }
85    };
86
87    private final AnimatorListenerAdapter mGoneOnEnd = new AnimatorListenerAdapter() {
88        @Override
89        public void onAnimationEnd(Animator animation) {
90            mDetail.setLayerType(View.LAYER_TYPE_NONE, null);
91            mDetail.setVisibility(View.GONE);
92            mBackground.resetTransition();
93            mAnimator = null;
94        };
95    };
96}
97