blob: a1cbd45a656e83d6bb80b556059807921bc8fffc [file] [log] [blame]
ztenghui4dc16b32014-03-25 09:58:34 -07001/*
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 android.animation;
18
John Reckd3de42c2014-07-15 14:29:33 -070019import android.view.RenderNodeAnimator;
ztenghui4dc16b32014-03-25 09:58:34 -070020import android.view.View;
21
ztenghui4dc16b32014-03-25 09:58:34 -070022/**
23 * Reveals a View with an animated clipping circle.
24 * The clipping is implemented efficiently by talking to a private reveal API on View.
25 * This hidden class currently only accessed by the {@link android.view.View}.
26 *
27 * @hide
28 */
29public class RevealAnimator extends ValueAnimator {
ztenghui4dc16b32014-03-25 09:58:34 -070030
John Reckd3de42c2014-07-15 14:29:33 -070031 private View mClipView;
32 private int mX, mY;
33 private boolean mInverseClip;
34 private float mStartRadius, mEndRadius;
35 private float mDelta;
36 private boolean mMayRunAsync;
ztenghui4dc16b32014-03-25 09:58:34 -070037
John Reckd3de42c2014-07-15 14:29:33 -070038 // If this is null, we are running on the UI thread driven by the base
39 // ValueAnimator class. If this is not null, forward requests on to this
40 // Animator instead.
41 private RenderNodeAnimator mRtAnimator;
ztenghui4dc16b32014-03-25 09:58:34 -070042
John Reckd3de42c2014-07-15 14:29:33 -070043 public RevealAnimator(View clipView, int x, int y,
44 float startRadius, float endRadius, boolean inverseClip) {
45 mClipView = clipView;
46 mStartRadius = startRadius;
47 mEndRadius = endRadius;
48 mDelta = endRadius - startRadius;
49 mX = x;
50 mY = y;
51 mInverseClip = inverseClip;
52 super.setValues(PropertyValuesHolder.ofFloat("radius", startRadius, endRadius));
ztenghui4dc16b32014-03-25 09:58:34 -070053 }
54
John Reckd3de42c2014-07-15 14:29:33 -070055 @Override
56 void animateValue(float fraction) {
57 super.animateValue(fraction);
58 fraction = getAnimatedFraction();
59 float radius = mStartRadius + (mDelta * fraction);
60 mClipView.setRevealClip(true, mInverseClip, mX, mY, radius);
61 }
62
63 @Override
64 protected void endAnimation(AnimationHandler handler) {
65 mClipView.setRevealClip(false, false, 0, 0, 0);
66 super.endAnimation(handler);
67 }
68
69 @Override
70 public void setAllowRunningAsynchronously(boolean mayRunAsync) {
71 mMayRunAsync = mayRunAsync;
72 }
73
74 private boolean canRunAsync() {
75 if (!mMayRunAsync) {
76 return false;
77 }
78 if (mUpdateListeners != null && mUpdateListeners.size() > 0) {
79 return false;
80 }
81 // TODO: Have RNA support this
82 if (getRepeatCount() != 0) {
83 return false;
84 }
85 return true;
86 }
87
88 @Override
89 public void start() {
90 if (mRtAnimator != null) {
91 mRtAnimator.end();
92 mRtAnimator = null;
93 }
94 if (canRunAsync()) {
95 mRtAnimator = new RenderNodeAnimator(mX, mY, mInverseClip, mStartRadius, mEndRadius);
96 mRtAnimator.setDuration(getDuration());
97 mRtAnimator.setInterpolator(getInterpolator());
98 mRtAnimator.setTarget(mClipView);
99 // TODO: Listeners
100 mRtAnimator.start();
101 } else {
102 super.start();
103 }
104 }
105
106 @Override
107 public void cancel() {
108 if (mRtAnimator != null) {
109 mRtAnimator.cancel();
110 } else {
111 super.cancel();
112 }
113 }
114
115 @Override
116 public void end() {
117 if (mRtAnimator != null) {
118 mRtAnimator.end();
119 } else {
120 super.end();
121 }
122 }
123
124 @Override
125 public void resume() {
126 if (mRtAnimator != null) {
127 // TODO: Support? Reject?
128 } else {
129 super.resume();
130 }
131 }
132
133 @Override
134 public void pause() {
135 if (mRtAnimator != null) {
136 // TODO: see resume()
137 } else {
138 super.pause();
139 }
140 }
141
142 @Override
143 public boolean isRunning() {
144 if (mRtAnimator != null) {
145 return mRtAnimator.isRunning();
146 } else {
147 return super.isRunning();
148 }
149 }
150
151 @Override
152 public boolean isStarted() {
153 if (mRtAnimator != null) {
154 return mRtAnimator.isStarted();
155 } else {
156 return super.isStarted();
157 }
158 }
159
160 @Override
161 public void reverse() {
162 if (mRtAnimator != null) {
163 // TODO support
164 } else {
165 super.reverse();
166 }
167 }
168
169 @Override
170 public ValueAnimator clone() {
171 RevealAnimator anim = (RevealAnimator) super.clone();
172 anim.mRtAnimator = null;
ztenghui4dc16b32014-03-25 09:58:34 -0700173 return anim;
174 }
175
John Reckd3de42c2014-07-15 14:29:33 -0700176 // ----------------------------------------
177 // All the things we don't allow
178 // ----------------------------------------
ztenghui4dc16b32014-03-25 09:58:34 -0700179
ztenghui4dc16b32014-03-25 09:58:34 -0700180 @Override
John Reckd3de42c2014-07-15 14:29:33 -0700181 public void setValues(PropertyValuesHolder... values) {
182 throw new IllegalStateException("Cannot change the values of RevealAnimator");
ztenghui4dc16b32014-03-25 09:58:34 -0700183 }
184
ztenghui4dc16b32014-03-25 09:58:34 -0700185 @Override
John Reckd3de42c2014-07-15 14:29:33 -0700186 public void setFloatValues(float... values) {
187 throw new IllegalStateException("Cannot change the values of RevealAnimator");
ztenghui4dc16b32014-03-25 09:58:34 -0700188 }
189
ztenghui4dc16b32014-03-25 09:58:34 -0700190 @Override
John Reckd3de42c2014-07-15 14:29:33 -0700191 public void setIntValues(int... values) {
192 throw new IllegalStateException("Cannot change the values of RevealAnimator");
ztenghui4dc16b32014-03-25 09:58:34 -0700193 }
194
John Reckd3de42c2014-07-15 14:29:33 -0700195 @Override
196 public void setObjectValues(Object... values) {
197 throw new IllegalStateException("Cannot change the values of RevealAnimator");
ztenghui4dc16b32014-03-25 09:58:34 -0700198 }
199
John Reckd3de42c2014-07-15 14:29:33 -0700200 @Override
201 public void setEvaluator(TypeEvaluator value) {
202 throw new IllegalStateException("Cannot change the evaluator of RevealAnimator");
ztenghui4dc16b32014-03-25 09:58:34 -0700203 }
204}