1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.qs;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.graphics.drawable.TransitionDrawable;
import android.view.View;
import android.view.ViewAnimationUtils;
/** Helper for quick settings detail panel clip animations. **/
public class QSDetailClipper {
private final View mDetail;
private final TransitionDrawable mBackground;
private Animator mAnimator;
public QSDetailClipper(View detail) {
mDetail = detail;
mBackground = (TransitionDrawable) detail.getBackground();
}
public void animateCircularClip(int x, int y, boolean in, AnimatorListener listener) {
updateCircularClip(true /* animate */, x, y, in, listener);
}
/**
* @param animate whether or not animation has a duration of 0. Either way, {@code listener}
* will be called.
* @param x x position where animation should originate
* @param y y position where animation should originate
* @param in whether animating in or out
* @param listener Animation listener. Called whether or not {@code animate} is true.
*/
public void updateCircularClip(boolean animate, int x, int y, boolean in,
AnimatorListener listener) {
if (mAnimator != null) {
mAnimator.cancel();
}
final int w = mDetail.getWidth() - x;
final int h = mDetail.getHeight() - y;
int innerR = 0;
if (x < 0 || w < 0 || y < 0 || h < 0) {
innerR = Math.abs(x);
innerR = Math.min(innerR, Math.abs(y));
innerR = Math.min(innerR, Math.abs(w));
innerR = Math.min(innerR, Math.abs(h));
}
int r = (int) Math.ceil(Math.sqrt(x * x + y * y));
r = (int) Math.max(r, Math.ceil(Math.sqrt(w * w + y * y)));
r = (int) Math.max(r, Math.ceil(Math.sqrt(w * w + h * h)));
r = (int) Math.max(r, Math.ceil(Math.sqrt(x * x + h * h)));
if (in) {
mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, innerR, r);
} else {
mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, r, innerR);
}
mAnimator.setDuration(animate ? (long) (mAnimator.getDuration() * 1.5) : 0);
if (listener != null) {
mAnimator.addListener(listener);
}
if (in) {
mBackground.startTransition(animate ? (int) (mAnimator.getDuration() * 0.6) : 0);
mAnimator.addListener(mVisibleOnStart);
} else {
mDetail.postDelayed(mReverseBackground,
animate ? (long) (mAnimator.getDuration() * 0.65) : 0);
mAnimator.addListener(mGoneOnEnd);
}
mAnimator.start();
}
private final Runnable mReverseBackground = new Runnable() {
@Override
public void run() {
if (mAnimator != null) {
mBackground.reverseTransition((int)(mAnimator.getDuration() * 0.35));
}
}
};
private final AnimatorListenerAdapter mVisibleOnStart = new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
mDetail.setVisibility(View.VISIBLE);
}
public void onAnimationEnd(Animator animation) {
mAnimator = null;
}
};
private final AnimatorListenerAdapter mGoneOnEnd = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mDetail.setVisibility(View.GONE);
mBackground.resetTransition();
mAnimator = null;
};
};
public void showBackground() {
mBackground.showSecondLayer();
}
/**
* Cancels the animator if it's running.
*/
public void cancelAnimator() {
if (mAnimator != null) {
mAnimator.cancel();
}
}
}
|