ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 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 | |
| 17 | package android.animation; |
| 18 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 19 | import android.view.RenderNodeAnimator; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 20 | import android.view.View; |
| 21 | |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 22 | /** |
| 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 | */ |
| 29 | public class RevealAnimator extends ValueAnimator { |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 30 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 31 | 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; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 37 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 38 | // 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; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 42 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 43 | 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)); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 53 | } |
| 54 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 55 | @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; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 173 | return anim; |
| 174 | } |
| 175 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 176 | // ---------------------------------------- |
| 177 | // All the things we don't allow |
| 178 | // ---------------------------------------- |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 179 | |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 180 | @Override |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 181 | public void setValues(PropertyValuesHolder... values) { |
| 182 | throw new IllegalStateException("Cannot change the values of RevealAnimator"); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 183 | } |
| 184 | |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 185 | @Override |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 186 | public void setFloatValues(float... values) { |
| 187 | throw new IllegalStateException("Cannot change the values of RevealAnimator"); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 188 | } |
| 189 | |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 190 | @Override |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 191 | public void setIntValues(int... values) { |
| 192 | throw new IllegalStateException("Cannot change the values of RevealAnimator"); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 193 | } |
| 194 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 195 | @Override |
| 196 | public void setObjectValues(Object... values) { |
| 197 | throw new IllegalStateException("Cannot change the values of RevealAnimator"); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 198 | } |
| 199 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame^] | 200 | @Override |
| 201 | public void setEvaluator(TypeEvaluator value) { |
| 202 | throw new IllegalStateException("Cannot change the evaluator of RevealAnimator"); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 203 | } |
| 204 | } |