blob: 033b99a22d51802c3a07ccd8d829b91de983dbae [file] [log] [blame]
Adam Powell637d3372010-08-25 14:37:03 -07001/*
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
17package android.widget;
18
Adam Powellc501db92014-05-08 12:50:10 -070019import android.content.res.TypedArray;
20import android.graphics.Paint;
21import android.graphics.PorterDuff;
22import android.graphics.PorterDuffXfermode;
Romain Guy9d849a22012-03-14 16:41:42 -070023import android.graphics.Rect;
Adam Powell89935e42011-08-31 14:26:12 -070024
Mindy Pereira4e30d892010-11-24 15:32:39 -080025import android.content.Context;
Adam Powell637d3372010-08-25 14:37:03 -070026import android.graphics.Canvas;
Adam Powell2897a6f2014-05-12 22:20:45 -070027import android.util.FloatMath;
Adam Powell637d3372010-08-25 14:37:03 -070028import android.view.animation.AnimationUtils;
29import android.view.animation.DecelerateInterpolator;
30import android.view.animation.Interpolator;
31
32/**
Adam Powell89935e42011-08-31 14:26:12 -070033 * 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 Powell637d3372010-08-25 14:37:03 -070049 */
Adam Powell89935e42011-08-31 14:26:12 -070050public class EdgeEffect {
Romain Guy9d849a22012-03-14 16:41:42 -070051 @SuppressWarnings("UnusedDeclaration")
Adam Powell89935e42011-08-31 14:26:12 -070052 private static final String TAG = "EdgeEffect";
Adam Powell637d3372010-08-25 14:37:03 -070053
54 // Time it will take the effect to fully recede in ms
Adam Powell710c4562014-09-04 15:36:01 -070055 private static final int RECEDE_TIME = 600;
Adam Powell637d3372010-08-25 14:37:03 -070056
Adam Powell89935e42011-08-31 14:26:12 -070057 // Time it will take before a pulled glow begins receding in ms
Adam Powell637d3372010-08-25 14:37:03 -070058 private static final int PULL_TIME = 167;
59
Adam Powell710c4562014-09-04 15:36:01 -070060 // Time it will take in ms for a pulled glow to decay to partial strength before release
61 private static final int PULL_DECAY_TIME = 2000;
62
63 private static final float MAX_ALPHA = 0.5f;
Adam Powell637d3372010-08-25 14:37:03 -070064
Adam Powell2897a6f2014-05-12 22:20:45 -070065 private static final float MAX_GLOW_SCALE = 2.f;
Adam Powell637d3372010-08-25 14:37:03 -070066
Adam Powellc501db92014-05-08 12:50:10 -070067 private static final float PULL_GLOW_BEGIN = 0.f;
Adam Powell637d3372010-08-25 14:37:03 -070068
69 // Minimum velocity that will be absorbed
70 private static final int MIN_VELOCITY = 100;
Christian Robertson2d1acfc2013-09-27 18:54:28 -070071 // Maximum velocity, clamps at this value
72 private static final int MAX_VELOCITY = 10000;
Adam Powell637d3372010-08-25 14:37:03 -070073
74 private static final float EPSILON = 0.001f;
75
Adam Powell2897a6f2014-05-12 22:20:45 -070076 private static final double ANGLE = Math.PI / 6;
77 private static final float SIN = (float) Math.sin(ANGLE);
78 private static final float COS = (float) Math.cos(ANGLE);
Adam Powell637d3372010-08-25 14:37:03 -070079
Adam Powell637d3372010-08-25 14:37:03 -070080 private float mGlowAlpha;
81 private float mGlowScaleY;
82
Adam Powell637d3372010-08-25 14:37:03 -070083 private float mGlowAlphaStart;
84 private float mGlowAlphaFinish;
85 private float mGlowScaleYStart;
86 private float mGlowScaleYFinish;
87
88 private long mStartTime;
89 private float mDuration;
90
91 private final Interpolator mInterpolator;
92
93 private static final int STATE_IDLE = 0;
94 private static final int STATE_PULL = 1;
95 private static final int STATE_ABSORB = 2;
96 private static final int STATE_RECEDE = 3;
97 private static final int STATE_PULL_DECAY = 4;
98
Adam Powell710c4562014-09-04 15:36:01 -070099 private static final float PULL_DISTANCE_ALPHA_GLOW_FACTOR = 0.8f;
Adam Powell637d3372010-08-25 14:37:03 -0700100
Adam Powell710c4562014-09-04 15:36:01 -0700101 private static final int VELOCITY_GLOW_FACTOR = 6;
Adam Powell637d3372010-08-25 14:37:03 -0700102
103 private int mState = STATE_IDLE;
104
105 private float mPullDistance;
Romain Guy9d849a22012-03-14 16:41:42 -0700106
107 private final Rect mBounds = new Rect();
Adam Powellc501db92014-05-08 12:50:10 -0700108 private final Paint mPaint = new Paint();
109 private float mRadius;
Adam Powell710c4562014-09-04 15:36:01 -0700110 private float mBaseGlowScale;
Adam Powellc501db92014-05-08 12:50:10 -0700111 private float mDisplacement = 0.5f;
112 private float mTargetDisplacement = 0.5f;
Romain Guya8bfeaf2012-03-15 13:14:14 -0700113
Adam Powell89935e42011-08-31 14:26:12 -0700114 /**
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 Powellc501db92014-05-08 12:50:10 -0700119 mPaint.setAntiAlias(true);
120 final TypedArray a = context.obtainStyledAttributes(
121 com.android.internal.R.styleable.EdgeEffect);
122 final int themeColor = a.getColor(
Alan Viverettedcf7b592014-06-03 14:43:38 -0700123 com.android.internal.R.styleable.EdgeEffect_colorPrimary, 0xff666666);
Adam Powellc501db92014-05-08 12:50:10 -0700124 a.recycle();
Adam Powell2897a6f2014-05-12 22:20:45 -0700125 mPaint.setColor((themeColor & 0xffffff) | 0x33000000);
Adam Powellc501db92014-05-08 12:50:10 -0700126 mPaint.setStyle(Paint.Style.FILL);
127 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
Adam Powell637d3372010-08-25 14:37:03 -0700128 mInterpolator = new DecelerateInterpolator();
129 }
130
Adam Powell89935e42011-08-31 14:26:12 -0700131 /**
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 Powell637d3372010-08-25 14:37:03 -0700137 public void setSize(int width, int height) {
Adam Powell2897a6f2014-05-12 22:20:45 -0700138 final float r = width * 0.75f / SIN;
139 final float y = COS * r;
Adam Powellc501db92014-05-08 12:50:10 -0700140 final float h = r - y;
Adam Powell710c4562014-09-04 15:36:01 -0700141 final float or = height * 0.75f / SIN;
142 final float oy = COS * or;
143 final float oh = or - oy;
144
Adam Powellc501db92014-05-08 12:50:10 -0700145 mRadius = r;
Adam Powell710c4562014-09-04 15:36:01 -0700146 mBaseGlowScale = h > 0 ? Math.min(oh / h, 1.f) : 1.f;
Adam Powell637d3372010-08-25 14:37:03 -0700147
Adam Powellc501db92014-05-08 12:50:10 -0700148 mBounds.set(mBounds.left, mBounds.top, width, (int) Math.min(height, h));
Romain Guy9d849a22012-03-14 16:41:42 -0700149 }
150
Romain Guy9d849a22012-03-14 16:41:42 -0700151 /**
Adam Powell89935e42011-08-31 14:26:12 -0700152 * Reports if this EdgeEffect's animation is finished. If this method returns false
153 * after a call to {@link #draw(Canvas)} the host widget should schedule another
154 * drawing pass to continue the animation.
155 *
156 * @return true if animation is finished, false if drawing should continue on the next frame.
157 */
Adam Powell637d3372010-08-25 14:37:03 -0700158 public boolean isFinished() {
159 return mState == STATE_IDLE;
160 }
161
Adam Powell89935e42011-08-31 14:26:12 -0700162 /**
163 * Immediately finish the current animation.
164 * After this call {@link #isFinished()} will return true.
165 */
Adam Powell637d3372010-08-25 14:37:03 -0700166 public void finish() {
167 mState = STATE_IDLE;
168 }
169
170 /**
Adam Powell89935e42011-08-31 14:26:12 -0700171 * A view should call this when content is pulled away from an edge by the user.
172 * This will update the state of the current visual effect and its associated animation.
173 * The host view should always {@link android.view.View#invalidate()} after this
174 * and draw the results accordingly.
Adam Powell637d3372010-08-25 14:37:03 -0700175 *
Adam Powellc501db92014-05-08 12:50:10 -0700176 * <p>Views using EdgeEffect should favor {@link #onPull(float, float)} when the displacement
177 * of the pull point is known.</p>
178 *
Adam Powell89935e42011-08-31 14:26:12 -0700179 * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to
180 * 1.f (full length of the view) or negative values to express change
181 * back toward the edge reached to initiate the effect.
Adam Powell637d3372010-08-25 14:37:03 -0700182 */
183 public void onPull(float deltaDistance) {
Adam Powellc501db92014-05-08 12:50:10 -0700184 onPull(deltaDistance, 0.5f);
185 }
186
187 /**
188 * A view should call this when content is pulled away from an edge by the user.
189 * This will update the state of the current visual effect and its associated animation.
190 * The host view should always {@link android.view.View#invalidate()} after this
191 * and draw the results accordingly.
192 *
193 * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to
194 * 1.f (full length of the view) or negative values to express change
195 * back toward the edge reached to initiate the effect.
196 * @param displacement The displacement from the starting side of the effect of the point
197 * initiating the pull. In the case of touch this is the finger position.
198 * Values may be from 0-1.
199 */
200 public void onPull(float deltaDistance, float displacement) {
Adam Powell637d3372010-08-25 14:37:03 -0700201 final long now = AnimationUtils.currentAnimationTimeMillis();
Adam Powellc501db92014-05-08 12:50:10 -0700202 mTargetDisplacement = displacement;
Adam Powell637d3372010-08-25 14:37:03 -0700203 if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) {
204 return;
205 }
206 if (mState != STATE_PULL) {
Adam Powellc501db92014-05-08 12:50:10 -0700207 mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY);
Adam Powell637d3372010-08-25 14:37:03 -0700208 }
209 mState = STATE_PULL;
210
211 mStartTime = now;
212 mDuration = PULL_TIME;
213
214 mPullDistance += deltaDistance;
Adam Powell637d3372010-08-25 14:37:03 -0700215
Adam Powell2897a6f2014-05-12 22:20:45 -0700216 final float absdd = Math.abs(deltaDistance);
Adam Powell637d3372010-08-25 14:37:03 -0700217 mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA,
Adam Powell2897a6f2014-05-12 22:20:45 -0700218 mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR));
Adam Powell637d3372010-08-25 14:37:03 -0700219
Adam Powell637d3372010-08-25 14:37:03 -0700220 if (mPullDistance == 0) {
Adam Powell2897a6f2014-05-12 22:20:45 -0700221 mGlowScaleY = mGlowScaleYStart = 0;
222 } else {
223 final float scale = Math.max(0, 1 - 1 /
224 FloatMath.sqrt(Math.abs(mPullDistance) * mBounds.height()) - 0.3f) / 0.7f;
Adam Powell637d3372010-08-25 14:37:03 -0700225
Adam Powell2897a6f2014-05-12 22:20:45 -0700226 mGlowScaleY = mGlowScaleYStart = scale;
227 }
Adam Powell637d3372010-08-25 14:37:03 -0700228
Adam Powell637d3372010-08-25 14:37:03 -0700229 mGlowAlphaFinish = mGlowAlpha;
230 mGlowScaleYFinish = mGlowScaleY;
231 }
232
233 /**
234 * Call when the object is released after being pulled.
Adam Powell89935e42011-08-31 14:26:12 -0700235 * This will begin the "decay" phase of the effect. After calling this method
236 * the host view should {@link android.view.View#invalidate()} and thereby
237 * draw the results accordingly.
Adam Powell637d3372010-08-25 14:37:03 -0700238 */
239 public void onRelease() {
240 mPullDistance = 0;
241
242 if (mState != STATE_PULL && mState != STATE_PULL_DECAY) {
243 return;
244 }
245
246 mState = STATE_RECEDE;
Adam Powell637d3372010-08-25 14:37:03 -0700247 mGlowAlphaStart = mGlowAlpha;
248 mGlowScaleYStart = mGlowScaleY;
249
Adam Powell637d3372010-08-25 14:37:03 -0700250 mGlowAlphaFinish = 0.f;
251 mGlowScaleYFinish = 0.f;
252
253 mStartTime = AnimationUtils.currentAnimationTimeMillis();
254 mDuration = RECEDE_TIME;
255 }
256
257 /**
258 * Call when the effect absorbs an impact at the given velocity.
Adam Powell89935e42011-08-31 14:26:12 -0700259 * Used when a fling reaches the scroll boundary.
260 *
261 * <p>When using a {@link android.widget.Scroller} or {@link android.widget.OverScroller},
262 * the method <code>getCurrVelocity</code> will provide a reasonable approximation
263 * to use here.</p>
Adam Powell637d3372010-08-25 14:37:03 -0700264 *
265 * @param velocity Velocity at impact in pixels per second.
266 */
267 public void onAbsorb(int velocity) {
268 mState = STATE_ABSORB;
Christian Robertson2d1acfc2013-09-27 18:54:28 -0700269 velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY);
Adam Powell637d3372010-08-25 14:37:03 -0700270
271 mStartTime = AnimationUtils.currentAnimationTimeMillis();
Christian Robertson2d1acfc2013-09-27 18:54:28 -0700272 mDuration = 0.15f + (velocity * 0.02f);
Adam Powell637d3372010-08-25 14:37:03 -0700273
Adam Powell637d3372010-08-25 14:37:03 -0700274 // The glow depends more on the velocity, and therefore starts out
275 // nearly invisible.
Christian Robertson2d1acfc2013-09-27 18:54:28 -0700276 mGlowAlphaStart = 0.3f;
Adam Powellc501db92014-05-08 12:50:10 -0700277 mGlowScaleYStart = Math.max(mGlowScaleY, 0.f);
Adam Powell637d3372010-08-25 14:37:03 -0700278
Adam Powell637d3372010-08-25 14:37:03 -0700279
280 // Growth for the size of the glow should be quadratic to properly
281 // respond
282 // to a user's scrolling speed. The faster the scrolling speed, the more
283 // intense the effect should be for both the size and the saturation.
Adam Powellc501db92014-05-08 12:50:10 -0700284 mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, 1.f);
Adam Powell637d3372010-08-25 14:37:03 -0700285 // Alpha should change for the glow as well as size.
286 mGlowAlphaFinish = Math.max(
287 mGlowAlphaStart, Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA));
Adam Powellc501db92014-05-08 12:50:10 -0700288 mTargetDisplacement = 0.5f;
Adam Powell637d3372010-08-25 14:37:03 -0700289 }
290
Brian Attwella2799182014-06-20 16:24:01 -0700291 /**
292 * Set the color of this edge effect in argb.
293 *
294 * @param color Color in argb
295 */
296 public void setColor(int color) {
297 mPaint.setColor(color);
298 }
299
300 /**
301 * Return the color of this edge effect in argb.
302 * @return The color of this edge effect in argb
303 */
304 public int getColor() {
305 return mPaint.getColor();
306 }
Adam Powell637d3372010-08-25 14:37:03 -0700307
308 /**
309 * Draw into the provided canvas. Assumes that the canvas has been rotated
310 * accordingly and the size has been set. The effect will be drawn the full
Adam Powell89935e42011-08-31 14:26:12 -0700311 * width of X=0 to X=width, beginning from Y=0 and extending to some factor <
Adam Powell637d3372010-08-25 14:37:03 -0700312 * 1.f of height.
313 *
314 * @param canvas Canvas to draw into
315 * @return true if drawing should continue beyond this frame to continue the
316 * animation
317 */
318 public boolean draw(Canvas canvas) {
319 update();
320
Adam Powellc501db92014-05-08 12:50:10 -0700321 final int count = canvas.save();
Mindy Pereiraa5531d72010-11-23 11:07:30 -0800322
Adam Powellc501db92014-05-08 12:50:10 -0700323 final float centerX = mBounds.centerX();
Chris Craik8eeb7292014-07-15 15:36:32 -0700324 final float centerY = mBounds.height() - mRadius;
Adam Powell2897a6f2014-05-12 22:20:45 -0700325
Adam Powell710c4562014-09-04 15:36:01 -0700326 canvas.scale(1.f, Math.min(mGlowScaleY, 1.f) * mBaseGlowScale, centerX, 0);
Adam Powellc501db92014-05-08 12:50:10 -0700327
328 final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f;
Adam Powell2897a6f2014-05-12 22:20:45 -0700329 float translateX = mBounds.width() * displacement / 2;
330
Adam Powell9be22452014-06-04 13:46:55 -0700331 canvas.clipRect(mBounds);
Adam Powell2897a6f2014-05-12 22:20:45 -0700332 canvas.translate(translateX, 0);
Adam Powell710c4562014-09-04 15:36:01 -0700333 mPaint.setAlpha((int) (0xff * mGlowAlpha));
Chris Craik8eeb7292014-07-15 15:36:32 -0700334 canvas.drawCircle(centerX, centerY, mRadius, mPaint);
Adam Powellc501db92014-05-08 12:50:10 -0700335 canvas.restoreToCount(count);
Mindy Pereira4e30d892010-11-24 15:32:39 -0800336
Adam Powellc501db92014-05-08 12:50:10 -0700337 boolean oneLastFrame = false;
338 if (mState == STATE_RECEDE && mGlowScaleY == 0) {
Romain Guy9d849a22012-03-14 16:41:42 -0700339 mState = STATE_IDLE;
Adam Powellc501db92014-05-08 12:50:10 -0700340 oneLastFrame = true;
Romain Guy9d849a22012-03-14 16:41:42 -0700341 }
342
Adam Powellc501db92014-05-08 12:50:10 -0700343 return mState != STATE_IDLE || oneLastFrame;
Adam Powell637d3372010-08-25 14:37:03 -0700344 }
345
Romain Guy9d849a22012-03-14 16:41:42 -0700346 /**
Adam Powellc501db92014-05-08 12:50:10 -0700347 * Return the maximum height that the edge effect will be drawn at given the original
348 * {@link #setSize(int, int) input size}.
349 * @return The maximum height of the edge effect
Romain Guy9d849a22012-03-14 16:41:42 -0700350 */
Adam Powellc501db92014-05-08 12:50:10 -0700351 public int getMaxHeight() {
Adam Powell2897a6f2014-05-12 22:20:45 -0700352 return (int) (mBounds.height() * MAX_GLOW_SCALE + 0.5f);
Romain Guy9d849a22012-03-14 16:41:42 -0700353 }
354
Adam Powell637d3372010-08-25 14:37:03 -0700355 private void update() {
356 final long time = AnimationUtils.currentAnimationTimeMillis();
357 final float t = Math.min((time - mStartTime) / mDuration, 1.f);
358
359 final float interp = mInterpolator.getInterpolation(t);
360
Adam Powell637d3372010-08-25 14:37:03 -0700361 mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp;
362 mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp;
Adam Powellc501db92014-05-08 12:50:10 -0700363 mDisplacement = (mDisplacement + mTargetDisplacement) / 2;
Adam Powell637d3372010-08-25 14:37:03 -0700364
365 if (t >= 1.f - EPSILON) {
366 switch (mState) {
367 case STATE_ABSORB:
368 mState = STATE_RECEDE;
369 mStartTime = AnimationUtils.currentAnimationTimeMillis();
370 mDuration = RECEDE_TIME;
371
Adam Powell637d3372010-08-25 14:37:03 -0700372 mGlowAlphaStart = mGlowAlpha;
373 mGlowScaleYStart = mGlowScaleY;
374
Adam Powellc501db92014-05-08 12:50:10 -0700375 // After absorb, the glow should fade to nothing.
Adam Powell637d3372010-08-25 14:37:03 -0700376 mGlowAlphaFinish = 0.f;
377 mGlowScaleYFinish = 0.f;
378 break;
379 case STATE_PULL:
Adam Powell710c4562014-09-04 15:36:01 -0700380 mState = STATE_PULL_DECAY;
381 mStartTime = AnimationUtils.currentAnimationTimeMillis();
382 mDuration = PULL_DECAY_TIME;
383
384 mGlowAlphaStart = mGlowAlpha;
385 mGlowScaleYStart = mGlowScaleY;
386
387 // After pull, the glow should fade to nothing.
388 mGlowAlphaFinish = 0.f;
389 mGlowScaleYFinish = 0.f;
Adam Powell637d3372010-08-25 14:37:03 -0700390 break;
391 case STATE_PULL_DECAY:
Daniel Mladenovic0b1ab3a2011-02-21 09:17:40 +0100392 mState = STATE_RECEDE;
Adam Powell637d3372010-08-25 14:37:03 -0700393 break;
394 case STATE_RECEDE:
395 mState = STATE_IDLE;
396 break;
397 }
398 }
399 }
400}