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