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