Adam Powell | 0a77ce2 | 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 | |
| 19 | import android.graphics.Canvas; |
| 20 | import android.graphics.drawable.Drawable; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 21 | import android.view.animation.AnimationUtils; |
| 22 | import android.view.animation.DecelerateInterpolator; |
| 23 | import android.view.animation.Interpolator; |
| 24 | |
| 25 | /** |
| 26 | * This class performs the glow effect used at the edges of scrollable widgets. |
| 27 | * @hide |
| 28 | */ |
| 29 | public class EdgeGlow { |
| 30 | private static final String TAG = "EdgeGlow"; |
| 31 | |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 32 | // Time it will take the effect to fully recede in ms |
| 33 | private static final int RECEDE_TIME = 1000; |
| 34 | |
| 35 | // Time it will take before a pulled glow begins receding |
| 36 | private static final int PULL_TIME = 250; |
| 37 | |
| 38 | // Time it will take for a pulled glow to decay to partial strength before release |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 39 | private static final int PULL_DECAY_TIME = 10000; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 40 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 41 | private static final float MAX_ALPHA = 1.f; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 42 | private static final float HELD_EDGE_ALPHA = 0.7f; |
| 43 | private static final float HELD_EDGE_SCALE_Y = 0.5f; |
| 44 | private static final float HELD_GLOW_ALPHA = 0.5f; |
| 45 | private static final float HELD_GLOW_SCALE_Y = 0.5f; |
| 46 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 47 | private static final float MAX_GLOW_HEIGHT = 3.f; |
Adam Powell | 5916882 | 2010-09-01 17:47:16 -0700 | [diff] [blame] | 48 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 49 | private static final float PULL_GLOW_BEGIN = 1.f; |
Adam Powell | 5916882 | 2010-09-01 17:47:16 -0700 | [diff] [blame] | 50 | private static final float PULL_EDGE_BEGIN = 0.6f; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 51 | |
| 52 | // Minimum velocity that will be absorbed |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 53 | private static final int MIN_VELOCITY = 100; |
| 54 | |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 55 | private static final float EPSILON = 0.001f; |
| 56 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 57 | private final Drawable mEdge; |
| 58 | private final Drawable mGlow; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 59 | private int mWidth; |
| 60 | private int mHeight; |
| 61 | |
| 62 | private float mEdgeAlpha; |
| 63 | private float mEdgeScaleY; |
| 64 | private float mGlowAlpha; |
| 65 | private float mGlowScaleY; |
| 66 | |
| 67 | private float mEdgeAlphaStart; |
| 68 | private float mEdgeAlphaFinish; |
| 69 | private float mEdgeScaleYStart; |
| 70 | private float mEdgeScaleYFinish; |
| 71 | private float mGlowAlphaStart; |
| 72 | private float mGlowAlphaFinish; |
| 73 | private float mGlowScaleYStart; |
| 74 | private float mGlowScaleYFinish; |
| 75 | |
| 76 | private long mStartTime; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 77 | private float mDuration; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 78 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 79 | private final Interpolator mInterpolator; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 80 | |
| 81 | private static final int STATE_IDLE = 0; |
| 82 | private static final int STATE_PULL = 1; |
| 83 | private static final int STATE_ABSORB = 2; |
| 84 | private static final int STATE_RECEDE = 3; |
| 85 | private static final int STATE_PULL_DECAY = 4; |
| 86 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 87 | // How much dragging should effect the height of the edge image. |
| 88 | // Number determined by user testing. |
| 89 | private static final int PULL_DISTANCE_EDGE_FACTOR = 5; |
| 90 | |
| 91 | // How much dragging should effect the height of the glow image. |
| 92 | // Number determined by user testing. |
Mindy Pereira | e6c4747 | 2010-10-01 16:37:06 -0700 | [diff] [blame] | 93 | private static final int PULL_DISTANCE_GLOW_FACTOR = 5; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 94 | |
| 95 | private static final int VELOCITY_EDGE_FACTOR = 8; |
| 96 | private static final int VELOCITY_GLOW_FACTOR = 16; |
| 97 | |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 98 | private int mState = STATE_IDLE; |
| 99 | |
| 100 | private float mPullDistance; |
| 101 | |
| 102 | public EdgeGlow(Drawable edge, Drawable glow) { |
| 103 | mEdge = edge; |
| 104 | mGlow = glow; |
| 105 | |
| 106 | mInterpolator = new DecelerateInterpolator(); |
| 107 | } |
| 108 | |
| 109 | public void setSize(int width, int height) { |
| 110 | mWidth = width; |
| 111 | mHeight = height; |
| 112 | } |
| 113 | |
| 114 | public boolean isFinished() { |
| 115 | return mState == STATE_IDLE; |
| 116 | } |
| 117 | |
Adam Powell | 5916882 | 2010-09-01 17:47:16 -0700 | [diff] [blame] | 118 | public void finish() { |
| 119 | mState = STATE_IDLE; |
| 120 | } |
| 121 | |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Call when the object is pulled by the user. |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 124 | * |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 125 | * @param deltaDistance Change in distance since the last call |
| 126 | */ |
| 127 | public void onPull(float deltaDistance) { |
| 128 | final long now = AnimationUtils.currentAnimationTimeMillis(); |
| 129 | if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) { |
| 130 | return; |
| 131 | } |
| 132 | if (mState != STATE_PULL) { |
| 133 | mGlowScaleY = PULL_GLOW_BEGIN; |
| 134 | } |
| 135 | mState = STATE_PULL; |
| 136 | |
| 137 | mStartTime = now; |
| 138 | mDuration = PULL_TIME; |
| 139 | |
| 140 | mPullDistance += deltaDistance; |
| 141 | float distance = Math.abs(mPullDistance); |
| 142 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 143 | mEdgeAlpha = mEdgeAlphaStart = Math.max(PULL_EDGE_BEGIN, Math.min(distance, MAX_ALPHA)); |
| 144 | mEdgeScaleY = mEdgeScaleYStart = Math.max( |
| 145 | HELD_EDGE_SCALE_Y, Math.min(distance * PULL_DISTANCE_EDGE_FACTOR, 1.f)); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 146 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 147 | mGlowAlpha = mGlowAlphaStart = Math.max( |
| 148 | 0.5f, Math.min(mGlowAlpha + Math.abs(deltaDistance), MAX_ALPHA)); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 149 | |
| 150 | float glowChange = Math.abs(deltaDistance); |
| 151 | if (deltaDistance > 0 && mPullDistance < 0) { |
| 152 | glowChange = -glowChange; |
| 153 | } |
| 154 | if (mPullDistance == 0) { |
| 155 | mGlowScaleY = 0; |
| 156 | } |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 157 | mGlowScaleY = mGlowScaleYStart = Math.max( |
| 158 | 0, mGlowScaleY + glowChange * PULL_DISTANCE_GLOW_FACTOR); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 159 | |
| 160 | mEdgeAlphaFinish = mEdgeAlpha; |
| 161 | mEdgeScaleYFinish = mEdgeScaleY; |
| 162 | mGlowAlphaFinish = mGlowAlpha; |
| 163 | mGlowScaleYFinish = mGlowScaleY; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Call when the object is released after being pulled. |
| 168 | */ |
| 169 | public void onRelease() { |
| 170 | mPullDistance = 0; |
| 171 | |
| 172 | if (mState != STATE_PULL && mState != STATE_PULL_DECAY) { |
| 173 | return; |
| 174 | } |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 175 | |
| 176 | mState = STATE_RECEDE; |
| 177 | mEdgeAlphaStart = mEdgeAlpha; |
| 178 | mEdgeScaleYStart = mEdgeScaleY; |
| 179 | mGlowAlphaStart = mGlowAlpha; |
| 180 | mGlowScaleYStart = mGlowScaleY; |
| 181 | |
| 182 | mEdgeAlphaFinish = 0.f; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 183 | mEdgeScaleYFinish = 0.f; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 184 | mGlowAlphaFinish = 0.f; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 185 | mGlowScaleYFinish = 0.f; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 186 | |
| 187 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 188 | mDuration = RECEDE_TIME; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Call when the effect absorbs an impact at the given velocity. |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 193 | * |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 194 | * @param velocity Velocity at impact in pixels per second. |
| 195 | */ |
| 196 | public void onAbsorb(int velocity) { |
| 197 | mState = STATE_ABSORB; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 198 | velocity = Math.max(MIN_VELOCITY, Math.abs(velocity)); |
| 199 | |
| 200 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 201 | mDuration = 0.1f + (velocity * 0.03f); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 202 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 203 | // The edge should always be at least partially visible, regardless |
| 204 | // of velocity. |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 205 | mEdgeAlphaStart = 0.5f; |
| 206 | mEdgeScaleYStart = 0.2f; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 207 | // The glow depends more on the velocity, and therefore starts out |
| 208 | // nearly invisible. |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 209 | mGlowAlphaStart = 0.5f; |
| 210 | mGlowScaleYStart = 0.f; |
| 211 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 212 | // Factor the velocity by 8. Testing on device shows this works best to |
| 213 | // reflect the strength of the user's scrolling. |
| 214 | mEdgeAlphaFinish = Math.max(0, Math.min(velocity * VELOCITY_EDGE_FACTOR, 1)); |
| 215 | // Edge should never get larger than the size of its asset. |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 216 | mEdgeScaleYFinish = 1.f; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 217 | |
| 218 | // Growth for the size of the glow should be quadratic to properly |
| 219 | // respond |
| 220 | // to a user's scrolling speed. The faster the scrolling speed, the more |
| 221 | // intense the effect should be for both the size and the saturation. |
| 222 | mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f), 1.75f); |
| 223 | // Alpha should change for the glow as well as size. |
| 224 | mGlowAlphaFinish = Math.max( |
| 225 | mGlowAlphaStart, Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA)); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 228 | |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 229 | /** |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 230 | * Draw into the provided canvas. Assumes that the canvas has been rotated |
| 231 | * accordingly and the size has been set. The effect will be drawn the full |
| 232 | * width of X=0 to X=width, emitting from Y=0 and extending to some factor < |
| 233 | * 1.f of height. |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 234 | * |
| 235 | * @param canvas Canvas to draw into |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 236 | * @return true if drawing should continue beyond this frame to continue the |
| 237 | * animation |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 238 | */ |
| 239 | public boolean draw(Canvas canvas) { |
| 240 | update(); |
| 241 | |
| 242 | final int edgeHeight = mEdge.getIntrinsicHeight(); |
| 243 | final int glowHeight = mGlow.getIntrinsicHeight(); |
| 244 | |
Adam Powell | 5916882 | 2010-09-01 17:47:16 -0700 | [diff] [blame] | 245 | final float distScale = (float) mHeight / mWidth; |
| 246 | |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 247 | mGlow.setAlpha((int) (Math.max(0, Math.min(mGlowAlpha, 1)) * 255)); |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 248 | // Width of the image should be 3 * the width of the screen. |
| 249 | // Should start off screen to the left. |
| 250 | mGlow.setBounds(-mWidth, 0, mWidth * 2, (int) Math.min( |
| 251 | glowHeight * mGlowScaleY * distScale * 0.6f, mHeight * MAX_GLOW_HEIGHT)); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 252 | mGlow.draw(canvas); |
| 253 | |
| 254 | mEdge.setAlpha((int) (Math.max(0, Math.min(mEdgeAlpha, 1)) * 255)); |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 255 | mEdge.setBounds(0, 0, mWidth, (int) (edgeHeight * mEdgeScaleY)); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 256 | mEdge.draw(canvas); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 257 | |
| 258 | return mState != STATE_IDLE; |
| 259 | } |
| 260 | |
| 261 | private void update() { |
| 262 | final long time = AnimationUtils.currentAnimationTimeMillis(); |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 263 | final float t = Math.min((time - mStartTime) / mDuration, 1.f); |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 264 | |
| 265 | final float interp = mInterpolator.getInterpolation(t); |
| 266 | |
| 267 | mEdgeAlpha = mEdgeAlphaStart + (mEdgeAlphaFinish - mEdgeAlphaStart) * interp; |
| 268 | mEdgeScaleY = mEdgeScaleYStart + (mEdgeScaleYFinish - mEdgeScaleYStart) * interp; |
| 269 | mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp; |
| 270 | mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp; |
| 271 | |
| 272 | if (t >= 1.f - EPSILON) { |
| 273 | switch (mState) { |
| 274 | case STATE_ABSORB: |
| 275 | mState = STATE_RECEDE; |
| 276 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 277 | mDuration = RECEDE_TIME; |
| 278 | |
| 279 | mEdgeAlphaStart = mEdgeAlpha; |
| 280 | mEdgeScaleYStart = mEdgeScaleY; |
| 281 | mGlowAlphaStart = mGlowAlpha; |
| 282 | mGlowScaleYStart = mGlowScaleY; |
| 283 | |
| 284 | mEdgeAlphaFinish = 0.f; |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 285 | mEdgeScaleYFinish = mEdgeScaleY; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 286 | mGlowAlphaFinish = 0.f; |
| 287 | mGlowScaleYFinish = mGlowScaleY; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 288 | break; |
| 289 | case STATE_PULL: |
| 290 | mState = STATE_PULL_DECAY; |
| 291 | mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 292 | mDuration = PULL_DECAY_TIME; |
| 293 | |
| 294 | mEdgeAlphaStart = mEdgeAlpha; |
| 295 | mEdgeScaleYStart = mEdgeScaleY; |
| 296 | mGlowAlphaStart = mGlowAlpha; |
| 297 | mGlowScaleYStart = mGlowScaleY; |
| 298 | |
Mindy Pereira | 40d3579 | 2010-09-20 09:12:41 -0700 | [diff] [blame] | 299 | // After a pull, the glow should fade to nothing. |
| 300 | mEdgeAlphaFinish = 0.f; |
| 301 | mEdgeScaleYFinish = 0.f; |
| 302 | mGlowAlphaFinish = 0.f; |
| 303 | mGlowScaleYFinish = 0.f; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 304 | break; |
| 305 | case STATE_PULL_DECAY: |
| 306 | // Do nothing; wait for release |
| 307 | break; |
| 308 | case STATE_RECEDE: |
| 309 | mState = STATE_IDLE; |
Adam Powell | 0a77ce2 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |