Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.widget; |
| 18 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 19 | import android.content.res.TypedArray; |
| 20 | import android.graphics.Paint; |
| 21 | import android.graphics.PorterDuff; |
| 22 | import android.graphics.PorterDuffXfermode; |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 24 | |
Mindy Pereira | 4e30d89 | 2010-11-24 15:32:39 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 26 | import android.graphics.Canvas; |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 27 | import android.util.FloatMath; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 28 | import android.view.animation.AnimationUtils; |
| 29 | import android.view.animation.DecelerateInterpolator; |
| 30 | import android.view.animation.Interpolator; |
| 31 | |
| 32 | /** |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 33 | * This class performs the graphical effect used at the edges of scrollable widgets |
| 34 | * when the user scrolls beyond the content bounds in 2D space. |
| 35 | * |
| 36 | * <p>EdgeEffect is stateful. Custom widgets using EdgeEffect should create an |
| 37 | * instance for each edge that should show the effect, feed it input data using |
| 38 | * the methods {@link #onAbsorb(int)}, {@link #onPull(float)}, and {@link #onRelease()}, |
| 39 | * and draw the effect using {@link #draw(Canvas)} in the widget's overridden |
| 40 | * {@link android.view.View#draw(Canvas)} method. If {@link #isFinished()} returns |
| 41 | * false after drawing, the edge effect's animation is not yet complete and the widget |
| 42 | * should schedule another drawing pass to continue the animation.</p> |
| 43 | * |
| 44 | * <p>When drawing, widgets should draw their main content and child views first, |
| 45 | * usually by invoking <code>super.draw(canvas)</code> from an overridden <code>draw</code> |
| 46 | * method. (This will invoke onDraw and dispatch drawing to child views as needed.) |
| 47 | * The edge effect may then be drawn on top of the view's content using the |
| 48 | * {@link #draw(Canvas)} method.</p> |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 49 | */ |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 50 | public class EdgeEffect { |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 51 | @SuppressWarnings("UnusedDeclaration") |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 52 | private static final String TAG = "EdgeEffect"; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 53 | |
| 54 | // Time it will take the effect to fully recede in ms |
| 55 | private static final int RECEDE_TIME = 1000; |
| 56 | |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 57 | // Time it will take before a pulled glow begins receding in ms |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 58 | private static final int PULL_TIME = 167; |
| 59 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 60 | private static final float MAX_ALPHA = 1.f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 61 | |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 62 | private static final float MAX_GLOW_SCALE = 2.f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 63 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 64 | private static final float PULL_GLOW_BEGIN = 0.f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 65 | |
| 66 | // Minimum velocity that will be absorbed |
| 67 | private static final int MIN_VELOCITY = 100; |
Christian Robertson | 2d1acfc | 2013-09-27 18:54:28 -0700 | [diff] [blame] | 68 | // Maximum velocity, clamps at this value |
| 69 | private static final int MAX_VELOCITY = 10000; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 70 | |
| 71 | private static final float EPSILON = 0.001f; |
| 72 | |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 73 | private static final double ANGLE = Math.PI / 6; |
| 74 | private static final float SIN = (float) Math.sin(ANGLE); |
| 75 | private static final float COS = (float) Math.cos(ANGLE); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 76 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 77 | private float mGlowAlpha; |
| 78 | private float mGlowScaleY; |
| 79 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 80 | private float mGlowAlphaStart; |
| 81 | private float mGlowAlphaFinish; |
| 82 | private float mGlowScaleYStart; |
| 83 | private float mGlowScaleYFinish; |
| 84 | |
| 85 | private long mStartTime; |
| 86 | private float mDuration; |
| 87 | |
| 88 | private final Interpolator mInterpolator; |
| 89 | |
| 90 | private static final int STATE_IDLE = 0; |
| 91 | private static final int STATE_PULL = 1; |
| 92 | private static final int STATE_ABSORB = 2; |
| 93 | private static final int STATE_RECEDE = 3; |
| 94 | private static final int STATE_PULL_DECAY = 4; |
| 95 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 96 | // How much dragging should effect the height of the glow image. |
| 97 | // Number determined by user testing. |
Mindy Pereira | 4e30d89 | 2010-11-24 15:32:39 -0800 | [diff] [blame] | 98 | private static final int PULL_DISTANCE_GLOW_FACTOR = 7; |
| 99 | private static final float PULL_DISTANCE_ALPHA_GLOW_FACTOR = 1.1f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 100 | |
Christian Robertson | 2d1acfc | 2013-09-27 18:54:28 -0700 | [diff] [blame] | 101 | private static final int VELOCITY_GLOW_FACTOR = 12; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 102 | |
| 103 | private int mState = STATE_IDLE; |
| 104 | |
| 105 | private float mPullDistance; |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 106 | |
| 107 | private final Rect mBounds = new Rect(); |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 108 | private final Paint mPaint = new Paint(); |
| 109 | private float mRadius; |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 110 | private float mBaseGlowHeight; |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 111 | private float mDisplacement = 0.5f; |
| 112 | private float mTargetDisplacement = 0.5f; |
Romain Guy | a8bfeaf | 2012-03-15 13:14:14 -0700 | [diff] [blame] | 113 | |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Construct a new EdgeEffect with a theme appropriate for the provided context. |
| 116 | * @param context Context used to provide theming and resource information for the EdgeEffect |
| 117 | */ |
| 118 | public EdgeEffect(Context context) { |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 119 | mPaint.setAntiAlias(true); |
| 120 | final TypedArray a = context.obtainStyledAttributes( |
| 121 | com.android.internal.R.styleable.EdgeEffect); |
| 122 | final int themeColor = a.getColor( |
Alan Viverette | dcf7b59 | 2014-06-03 14:43:38 -0700 | [diff] [blame] | 123 | com.android.internal.R.styleable.EdgeEffect_colorPrimary, 0xff666666); |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 124 | a.recycle(); |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 125 | mPaint.setColor((themeColor & 0xffffff) | 0x33000000); |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 126 | mPaint.setStyle(Paint.Style.FILL); |
| 127 | mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 128 | mInterpolator = new DecelerateInterpolator(); |
| 129 | } |
| 130 | |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Set the size of this edge effect in pixels. |
| 133 | * |
| 134 | * @param width Effect width in pixels |
| 135 | * @param height Effect height in pixels |
| 136 | */ |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 137 | public void setSize(int width, int height) { |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 138 | final float r = width * 0.75f / SIN; |
| 139 | final float y = COS * r; |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 140 | final float h = r - y; |
| 141 | mRadius = r; |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 142 | mBaseGlowHeight = h; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 143 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 144 | mBounds.set(mBounds.left, mBounds.top, width, (int) Math.min(height, h)); |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 147 | /** |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 148 | * Reports if this EdgeEffect's animation is finished. If this method returns false |
| 149 | * after a call to {@link #draw(Canvas)} the host widget should schedule another |
| 150 | * drawing pass to continue the animation. |
| 151 | * |
| 152 | * @return true if animation is finished, false if drawing should continue on the next frame. |
| 153 | */ |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 154 | public boolean isFinished() { |
| 155 | return mState == STATE_IDLE; |
| 156 | } |
| 157 | |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Immediately finish the current animation. |
| 160 | * After this call {@link #isFinished()} will return true. |
| 161 | */ |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 162 | public void finish() { |
| 163 | mState = STATE_IDLE; |
| 164 | } |
| 165 | |
| 166 | /** |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 167 | * A view should call this when content is pulled away from an edge by the user. |
| 168 | * This will update the state of the current visual effect and its associated animation. |
| 169 | * The host view should always {@link android.view.View#invalidate()} after this |
| 170 | * and draw the results accordingly. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 171 | * |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 172 | * <p>Views using EdgeEffect should favor {@link #onPull(float, float)} when the displacement |
| 173 | * of the pull point is known.</p> |
| 174 | * |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 175 | * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to |
| 176 | * 1.f (full length of the view) or negative values to express change |
| 177 | * back toward the edge reached to initiate the effect. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 178 | */ |
| 179 | public void onPull(float deltaDistance) { |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 180 | onPull(deltaDistance, 0.5f); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * A view should call this when content is pulled away from an edge by the user. |
| 185 | * This will update the state of the current visual effect and its associated animation. |
| 186 | * The host view should always {@link android.view.View#invalidate()} after this |
| 187 | * and draw the results accordingly. |
| 188 | * |
| 189 | * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to |
| 190 | * 1.f (full length of the view) or negative values to express change |
| 191 | * back toward the edge reached to initiate the effect. |
| 192 | * @param displacement The displacement from the starting side of the effect of the point |
| 193 | * initiating the pull. In the case of touch this is the finger position. |
| 194 | * Values may be from 0-1. |
| 195 | */ |
| 196 | public void onPull(float deltaDistance, float displacement) { |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 197 | final long now = AnimationUtils.currentAnimationTimeMillis(); |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 198 | mTargetDisplacement = displacement; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 199 | if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) { |
| 200 | return; |
| 201 | } |
| 202 | if (mState != STATE_PULL) { |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 203 | mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 204 | } |
| 205 | mState = STATE_PULL; |
| 206 | |
| 207 | mStartTime = now; |
| 208 | mDuration = PULL_TIME; |
| 209 | |
| 210 | mPullDistance += deltaDistance; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 211 | |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 212 | final float absdd = Math.abs(deltaDistance); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 213 | mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA, |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 214 | mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR)); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 215 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 216 | if (mPullDistance == 0) { |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 217 | mGlowScaleY = mGlowScaleYStart = 0; |
| 218 | } else { |
| 219 | final float scale = Math.max(0, 1 - 1 / |
| 220 | FloatMath.sqrt(Math.abs(mPullDistance) * mBounds.height()) - 0.3f) / 0.7f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 221 | |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 222 | mGlowScaleY = mGlowScaleYStart = scale; |
| 223 | } |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 224 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 225 | mGlowAlphaFinish = mGlowAlpha; |
| 226 | mGlowScaleYFinish = mGlowScaleY; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Call when the object is released after being pulled. |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 231 | * This will begin the "decay" phase of the effect. After calling this method |
| 232 | * the host view should {@link android.view.View#invalidate()} and thereby |
| 233 | * draw the results accordingly. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 234 | */ |
| 235 | public void onRelease() { |
| 236 | mPullDistance = 0; |
| 237 | |
| 238 | if (mState != STATE_PULL && mState != STATE_PULL_DECAY) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | mState = STATE_RECEDE; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 243 | mGlowAlphaStart = mGlowAlpha; |
| 244 | mGlowScaleYStart = mGlowScaleY; |
| 245 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 246 | mGlowAlphaFinish = 0.f; |
| 247 | mGlowScaleYFinish = 0.f; |
| 248 | |
| 249 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 250 | mDuration = RECEDE_TIME; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Call when the effect absorbs an impact at the given velocity. |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 255 | * Used when a fling reaches the scroll boundary. |
| 256 | * |
| 257 | * <p>When using a {@link android.widget.Scroller} or {@link android.widget.OverScroller}, |
| 258 | * the method <code>getCurrVelocity</code> will provide a reasonable approximation |
| 259 | * to use here.</p> |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 260 | * |
| 261 | * @param velocity Velocity at impact in pixels per second. |
| 262 | */ |
| 263 | public void onAbsorb(int velocity) { |
| 264 | mState = STATE_ABSORB; |
Christian Robertson | 2d1acfc | 2013-09-27 18:54:28 -0700 | [diff] [blame] | 265 | velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 266 | |
| 267 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
Christian Robertson | 2d1acfc | 2013-09-27 18:54:28 -0700 | [diff] [blame] | 268 | mDuration = 0.15f + (velocity * 0.02f); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 269 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 270 | // The glow depends more on the velocity, and therefore starts out |
| 271 | // nearly invisible. |
Christian Robertson | 2d1acfc | 2013-09-27 18:54:28 -0700 | [diff] [blame] | 272 | mGlowAlphaStart = 0.3f; |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 273 | mGlowScaleYStart = Math.max(mGlowScaleY, 0.f); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 274 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 275 | |
| 276 | // Growth for the size of the glow should be quadratic to properly |
| 277 | // respond |
| 278 | // to a user's scrolling speed. The faster the scrolling speed, the more |
| 279 | // intense the effect should be for both the size and the saturation. |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 280 | mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, 1.f); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 281 | // Alpha should change for the glow as well as size. |
| 282 | mGlowAlphaFinish = Math.max( |
| 283 | mGlowAlphaStart, Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA)); |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 284 | mTargetDisplacement = 0.5f; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Brian Attwell | a279918 | 2014-06-20 16:24:01 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Set the color of this edge effect in argb. |
| 289 | * |
| 290 | * @param color Color in argb |
| 291 | */ |
| 292 | public void setColor(int color) { |
| 293 | mPaint.setColor(color); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Return the color of this edge effect in argb. |
| 298 | * @return The color of this edge effect in argb |
| 299 | */ |
| 300 | public int getColor() { |
| 301 | return mPaint.getColor(); |
| 302 | } |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Draw into the provided canvas. Assumes that the canvas has been rotated |
| 306 | * accordingly and the size has been set. The effect will be drawn the full |
Adam Powell | 89935e4 | 2011-08-31 14:26:12 -0700 | [diff] [blame] | 307 | * width of X=0 to X=width, beginning from Y=0 and extending to some factor < |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 308 | * 1.f of height. |
| 309 | * |
| 310 | * @param canvas Canvas to draw into |
| 311 | * @return true if drawing should continue beyond this frame to continue the |
| 312 | * animation |
| 313 | */ |
| 314 | public boolean draw(Canvas canvas) { |
| 315 | update(); |
| 316 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 317 | final int count = canvas.save(); |
Mindy Pereira | a5531d7 | 2010-11-23 11:07:30 -0800 | [diff] [blame] | 318 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 319 | final float centerX = mBounds.centerX(); |
Chris Craik | 8eeb729 | 2014-07-15 15:36:32 -0700 | [diff] [blame] | 320 | final float centerY = mBounds.height() - mRadius; |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 321 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 322 | canvas.scale(1.f, Math.min(mGlowScaleY, 1.f), centerX, 0); |
| 323 | |
| 324 | final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f; |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 325 | float translateX = mBounds.width() * displacement / 2; |
| 326 | |
Adam Powell | 9be2245 | 2014-06-04 13:46:55 -0700 | [diff] [blame] | 327 | canvas.clipRect(mBounds); |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 328 | canvas.translate(translateX, 0); |
Chris Craik | 8eeb729 | 2014-07-15 15:36:32 -0700 | [diff] [blame] | 329 | canvas.drawCircle(centerX, centerY, mRadius, mPaint); |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 330 | canvas.restoreToCount(count); |
Mindy Pereira | 4e30d89 | 2010-11-24 15:32:39 -0800 | [diff] [blame] | 331 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 332 | boolean oneLastFrame = false; |
| 333 | if (mState == STATE_RECEDE && mGlowScaleY == 0) { |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 334 | mState = STATE_IDLE; |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 335 | oneLastFrame = true; |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 338 | return mState != STATE_IDLE || oneLastFrame; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 341 | /** |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 342 | * Return the maximum height that the edge effect will be drawn at given the original |
| 343 | * {@link #setSize(int, int) input size}. |
| 344 | * @return The maximum height of the edge effect |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 345 | */ |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 346 | public int getMaxHeight() { |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 347 | return (int) (mBounds.height() * MAX_GLOW_SCALE + 0.5f); |
Romain Guy | 9d849a2 | 2012-03-14 16:41:42 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 350 | private void update() { |
| 351 | final long time = AnimationUtils.currentAnimationTimeMillis(); |
| 352 | final float t = Math.min((time - mStartTime) / mDuration, 1.f); |
| 353 | |
| 354 | final float interp = mInterpolator.getInterpolation(t); |
| 355 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 356 | mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp; |
| 357 | mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp; |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 358 | mDisplacement = (mDisplacement + mTargetDisplacement) / 2; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 359 | |
| 360 | if (t >= 1.f - EPSILON) { |
| 361 | switch (mState) { |
| 362 | case STATE_ABSORB: |
| 363 | mState = STATE_RECEDE; |
| 364 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 365 | mDuration = RECEDE_TIME; |
| 366 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 367 | mGlowAlphaStart = mGlowAlpha; |
| 368 | mGlowScaleYStart = mGlowScaleY; |
| 369 | |
Adam Powell | c501db9 | 2014-05-08 12:50:10 -0700 | [diff] [blame] | 370 | // After absorb, the glow should fade to nothing. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 371 | mGlowAlphaFinish = 0.f; |
| 372 | mGlowScaleYFinish = 0.f; |
| 373 | break; |
| 374 | case STATE_PULL: |
Adam Powell | 2897a6f | 2014-05-12 22:20:45 -0700 | [diff] [blame] | 375 | // Hold in this state until explicitly released. |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 376 | break; |
| 377 | case STATE_PULL_DECAY: |
Daniel Mladenovic | 0b1ab3a | 2011-02-21 09:17:40 +0100 | [diff] [blame] | 378 | mState = STATE_RECEDE; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 379 | break; |
| 380 | case STATE_RECEDE: |
| 381 | mState = STATE_IDLE; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |