2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3.util;
19 import android.graphics.Canvas;
20 import android.graphics.Paint;
21 import android.graphics.Rect;
22 import android.view.animation.AnimationUtils;
23 import android.view.animation.DecelerateInterpolator;
24 import android.view.animation.Interpolator;
27 * This class differs from the framework {@link android.widget.EdgeEffect}:
28 * 1) It does not use PorterDuffXfermode
29 * 2) The width to radius factor is smaller (0.5 instead of 0.75)
31 public class LauncherEdgeEffect {
33 // Time it will take the effect to fully recede in ms
34 private static final int RECEDE_TIME = 600;
36 // Time it will take before a pulled glow begins receding in ms
37 private static final int PULL_TIME = 167;
39 // Time it will take in ms for a pulled glow to decay to partial strength before release
40 private static final int PULL_DECAY_TIME = 2000;
42 private static final float MAX_ALPHA = 0.5f;
44 private static final float MAX_GLOW_SCALE = 2.f;
46 private static final float PULL_GLOW_BEGIN = 0.f;
48 // Minimum velocity that will be absorbed
49 private static final int MIN_VELOCITY = 100;
50 // Maximum velocity, clamps at this value
51 private static final int MAX_VELOCITY = 10000;
53 private static final float EPSILON = 0.001f;
55 private static final double ANGLE = Math.PI / 6;
56 private static final float SIN = (float) Math.sin(ANGLE);
57 private static final float COS = (float) Math.cos(ANGLE);
59 private float mGlowAlpha;
60 private float mGlowScaleY;
62 private float mGlowAlphaStart;
63 private float mGlowAlphaFinish;
64 private float mGlowScaleYStart;
65 private float mGlowScaleYFinish;
67 private long mStartTime;
68 private float mDuration;
70 private final Interpolator mInterpolator;
72 private static final int STATE_IDLE = 0;
73 private static final int STATE_PULL = 1;
74 private static final int STATE_ABSORB = 2;
75 private static final int STATE_RECEDE = 3;
76 private static final int STATE_PULL_DECAY = 4;
78 private static final float PULL_DISTANCE_ALPHA_GLOW_FACTOR = 0.8f;
80 private static final int VELOCITY_GLOW_FACTOR = 6;
82 private int mState = STATE_IDLE;
84 private float mPullDistance;
86 private final Rect mBounds = new Rect();
87 private final Paint mPaint = new Paint();
88 private float mRadius;
89 private float mBaseGlowScale;
90 private float mDisplacement = 0.5f;
91 private float mTargetDisplacement = 0.5f;
94 * Construct a new EdgeEffect with a theme appropriate for the provided context.
96 public LauncherEdgeEffect() {
97 mPaint.setAntiAlias(true);
98 mPaint.setStyle(Paint.Style.FILL);
99 mInterpolator = new DecelerateInterpolator();
103 * Set the size of this edge effect in pixels.
105 * @param width Effect width in pixels
106 * @param height Effect height in pixels
108 public void setSize(int width, int height) {
109 final float r = width * 0.5f / SIN;
110 final float y = COS * r;
111 final float h = r - y;
112 final float or = height * 0.75f / SIN;
113 final float oy = COS * or;
114 final float oh = or - oy;
117 mBaseGlowScale = h > 0 ? Math.min(oh / h, 1.f) : 1.f;
119 mBounds.set(mBounds.left, mBounds.top, width, (int) Math.min(height, h));
123 * Reports if this EdgeEffect's animation is finished. If this method returns false
124 * after a call to {@link #draw(Canvas)} the host widget should schedule another
125 * drawing pass to continue the animation.
127 * @return true if animation is finished, false if drawing should continue on the next frame.
129 public boolean isFinished() {
130 return mState == STATE_IDLE;
134 * Immediately finish the current animation.
135 * After this call {@link #isFinished()} will return true.
137 public void finish() {
142 * A view should call this when content is pulled away from an edge by the user.
143 * This will update the state of the current visual effect and its associated animation.
144 * The host view should always {@link android.view.View#invalidate()} after this
145 * and draw the results accordingly.
147 * <p>Views using EdgeEffect should favor {@link #onPull(float, float)} when the displacement
148 * of the pull point is known.</p>
150 * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to
151 * 1.f (full length of the view) or negative values to express change
152 * back toward the edge reached to initiate the effect.
154 public void onPull(float deltaDistance) {
155 onPull(deltaDistance, 0.5f);
159 * A view should call this when content is pulled away from an edge by the user.
160 * This will update the state of the current visual effect and its associated animation.
161 * The host view should always {@link android.view.View#invalidate()} after this
162 * and draw the results accordingly.
164 * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to
165 * 1.f (full length of the view) or negative values to express change
166 * back toward the edge reached to initiate the effect.
167 * @param displacement The displacement from the starting side of the effect of the point
168 * initiating the pull. In the case of touch this is the finger position.
169 * Values may be from 0-1.
171 public void onPull(float deltaDistance, float displacement) {
172 final long now = AnimationUtils.currentAnimationTimeMillis();
173 mTargetDisplacement = displacement;
174 if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) {
177 if (mState != STATE_PULL) {
178 mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY);
183 mDuration = PULL_TIME;
185 mPullDistance += deltaDistance;
187 final float absdd = Math.abs(deltaDistance);
188 mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA,
189 mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR));
191 if (mPullDistance == 0) {
192 mGlowScaleY = mGlowScaleYStart = 0;
194 final float scale = (float) (Math.max(0, 1 - 1 /
195 Math.sqrt(Math.abs(mPullDistance) * mBounds.height()) - 0.3d) / 0.7d);
197 mGlowScaleY = mGlowScaleYStart = scale;
200 mGlowAlphaFinish = mGlowAlpha;
201 mGlowScaleYFinish = mGlowScaleY;
205 * Call when the object is released after being pulled.
206 * This will begin the "decay" phase of the effect. After calling this method
207 * the host view should {@link android.view.View#invalidate()} and thereby
208 * draw the results accordingly.
210 public void onRelease() {
213 if (mState != STATE_PULL && mState != STATE_PULL_DECAY) {
217 mState = STATE_RECEDE;
218 mGlowAlphaStart = mGlowAlpha;
219 mGlowScaleYStart = mGlowScaleY;
221 mGlowAlphaFinish = 0.f;
222 mGlowScaleYFinish = 0.f;
224 mStartTime = AnimationUtils.currentAnimationTimeMillis();
225 mDuration = RECEDE_TIME;
229 * Call when the effect absorbs an impact at the given velocity.
230 * Used when a fling reaches the scroll boundary.
232 * <p>When using a {@link android.widget.Scroller} or {@link android.widget.OverScroller},
233 * the method <code>getCurrVelocity</code> will provide a reasonable approximation
236 * @param velocity Velocity at impact in pixels per second.
238 public void onAbsorb(int velocity) {
239 mState = STATE_ABSORB;
240 velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY);
242 mStartTime = AnimationUtils.currentAnimationTimeMillis();
243 mDuration = 0.15f + (velocity * 0.02f);
245 // The glow depends more on the velocity, and therefore starts out
247 mGlowAlphaStart = 0.3f;
248 mGlowScaleYStart = Math.max(mGlowScaleY, 0.f);
251 // Growth for the size of the glow should be quadratic to properly
253 // to a user's scrolling speed. The faster the scrolling speed, the more
254 // intense the effect should be for both the size and the saturation.
255 mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, 1.f);
256 // Alpha should change for the glow as well as size.
257 mGlowAlphaFinish = Math.max(
258 mGlowAlphaStart, Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA));
259 mTargetDisplacement = 0.5f;
263 * Set the color of this edge effect in argb.
265 * @param color Color in argb
267 public void setColor(int color) {
268 mPaint.setColor(color);
272 * Return the color of this edge effect in argb.
273 * @return The color of this edge effect in argb
275 public int getColor() {
276 return mPaint.getColor();
280 * Draw into the provided canvas. Assumes that the canvas has been rotated
281 * accordingly and the size has been set. The effect will be drawn the full
282 * width of X=0 to X=width, beginning from Y=0 and extending to some factor <
285 * @param canvas Canvas to draw into
286 * @return true if drawing should continue beyond this frame to continue the
289 public boolean draw(Canvas canvas) {
292 final float centerX = mBounds.centerX();
293 final float centerY = mBounds.height() - mRadius;
295 canvas.scale(1.f, Math.min(mGlowScaleY, 1.f) * mBaseGlowScale, centerX, 0);
297 final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f;
298 float translateX = mBounds.width() * displacement / 2;
299 mPaint.setAlpha((int) (0xff * mGlowAlpha));
300 canvas.drawCircle(centerX + translateX, centerY, mRadius, mPaint);
302 boolean oneLastFrame = false;
303 if (mState == STATE_RECEDE && mGlowScaleY == 0) {
308 return mState != STATE_IDLE || oneLastFrame;
312 * Return the maximum height that the edge effect will be drawn at given the original
313 * {@link #setSize(int, int) input size}.
314 * @return The maximum height of the edge effect
316 public int getMaxHeight() {
317 return (int) (mBounds.height() * MAX_GLOW_SCALE + 0.5f);
320 private void update() {
321 final long time = AnimationUtils.currentAnimationTimeMillis();
322 final float t = Math.min((time - mStartTime) / mDuration, 1.f);
324 final float interp = mInterpolator.getInterpolation(t);
326 mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp;
327 mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp;
328 mDisplacement = (mDisplacement + mTargetDisplacement) / 2;
330 if (t >= 1.f - EPSILON) {
333 mState = STATE_RECEDE;
334 mStartTime = AnimationUtils.currentAnimationTimeMillis();
335 mDuration = RECEDE_TIME;
337 mGlowAlphaStart = mGlowAlpha;
338 mGlowScaleYStart = mGlowScaleY;
340 // After absorb, the glow should fade to nothing.
341 mGlowAlphaFinish = 0.f;
342 mGlowScaleYFinish = 0.f;
345 mState = STATE_PULL_DECAY;
346 mStartTime = AnimationUtils.currentAnimationTimeMillis();
347 mDuration = PULL_DECAY_TIME;
349 mGlowAlphaStart = mGlowAlpha;
350 mGlowScaleYStart = mGlowScaleY;
352 // After pull, the glow should fade to nothing.
353 mGlowAlphaFinish = 0.f;
354 mGlowScaleYFinish = 0.f;
356 case STATE_PULL_DECAY:
357 mState = STATE_RECEDE;