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; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 33 | private float mStartRadius, mEndRadius; |
| 34 | private float mDelta; |
| 35 | private boolean mMayRunAsync; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 36 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 37 | // If this is null, we are running on the UI thread driven by the base |
| 38 | // ValueAnimator class. If this is not null, forward requests on to this |
| 39 | // Animator instead. |
| 40 | private RenderNodeAnimator mRtAnimator; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 41 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 42 | public RevealAnimator(View clipView, int x, int y, |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame^] | 43 | float startRadius, float endRadius) { |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 44 | mClipView = clipView; |
| 45 | mStartRadius = startRadius; |
| 46 | mEndRadius = endRadius; |
| 47 | mDelta = endRadius - startRadius; |
| 48 | mX = x; |
| 49 | mY = y; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 50 | super.setValues(PropertyValuesHolder.ofFloat("radius", startRadius, endRadius)); |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 51 | } |
| 52 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 53 | @Override |
| 54 | void animateValue(float fraction) { |
| 55 | super.animateValue(fraction); |
| 56 | fraction = getAnimatedFraction(); |
| 57 | float radius = mStartRadius + (mDelta * fraction); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame^] | 58 | mClipView.setRevealClip(true, mX, mY, radius); |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void endAnimation(AnimationHandler handler) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame^] | 63 | mClipView.setRevealClip(false, 0, 0, 0); |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 64 | super.endAnimation(handler); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void setAllowRunningAsynchronously(boolean mayRunAsync) { |
| 69 | mMayRunAsync = mayRunAsync; |
| 70 | } |
| 71 | |
| 72 | private boolean canRunAsync() { |
| 73 | if (!mMayRunAsync) { |
| 74 | return false; |
| 75 | } |
| 76 | if (mUpdateListeners != null && mUpdateListeners.size() > 0) { |
| 77 | return false; |
| 78 | } |
| 79 | // TODO: Have RNA support this |
| 80 | if (getRepeatCount() != 0) { |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void start() { |
| 88 | if (mRtAnimator != null) { |
| 89 | mRtAnimator.end(); |
| 90 | mRtAnimator = null; |
| 91 | } |
| 92 | if (canRunAsync()) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame^] | 93 | mRtAnimator = new RenderNodeAnimator(mX, mY, mStartRadius, mEndRadius); |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 94 | mRtAnimator.setDuration(getDuration()); |
| 95 | mRtAnimator.setInterpolator(getInterpolator()); |
| 96 | mRtAnimator.setTarget(mClipView); |
| 97 | // TODO: Listeners |
| 98 | mRtAnimator.start(); |
| 99 | } else { |
| 100 | super.start(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void cancel() { |
| 106 | if (mRtAnimator != null) { |
| 107 | mRtAnimator.cancel(); |
| 108 | } else { |
| 109 | super.cancel(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void end() { |
| 115 | if (mRtAnimator != null) { |
| 116 | mRtAnimator.end(); |
| 117 | } else { |
| 118 | super.end(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void resume() { |
| 124 | if (mRtAnimator != null) { |
| 125 | // TODO: Support? Reject? |
| 126 | } else { |
| 127 | super.resume(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void pause() { |
| 133 | if (mRtAnimator != null) { |
| 134 | // TODO: see resume() |
| 135 | } else { |
| 136 | super.pause(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean isRunning() { |
| 142 | if (mRtAnimator != null) { |
| 143 | return mRtAnimator.isRunning(); |
| 144 | } else { |
| 145 | return super.isRunning(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean isStarted() { |
| 151 | if (mRtAnimator != null) { |
| 152 | return mRtAnimator.isStarted(); |
| 153 | } else { |
| 154 | return super.isStarted(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void reverse() { |
| 160 | if (mRtAnimator != null) { |
| 161 | // TODO support |
| 162 | } else { |
| 163 | super.reverse(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
John Reck | c01bd11 | 2014-07-18 16:22:09 -0700 | [diff] [blame] | 168 | public RevealAnimator clone() { |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 169 | RevealAnimator anim = (RevealAnimator) super.clone(); |
| 170 | anim.mRtAnimator = null; |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 171 | return anim; |
| 172 | } |
ztenghui | 4dc16b3 | 2014-03-25 09:58:34 -0700 | [diff] [blame] | 173 | } |