| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.animation.ValueAnimator; |
| 20 |
import android.annotation.ColorInt; |
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.compat.Compatibility; |
| 25 |
import android.compat.annotation.ChangeId; |
| 26 |
import android.compat.annotation.EnabledSince; |
| 27 |
import android.compat.annotation.UnsupportedAppUsage; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.res.TypedArray; |
| 30 |
import android.graphics.BlendMode; |
| 31 |
import android.graphics.Canvas; |
| 32 |
import android.graphics.Matrix; |
| 33 |
import android.graphics.Paint; |
| 34 |
import android.graphics.RecordingCanvas; |
| 35 |
import android.graphics.Rect; |
| 36 |
import android.graphics.RenderNode; |
| 37 |
import android.os.Build; |
| 38 |
import android.util.AttributeSet; |
| 39 |
import android.view.animation.AnimationUtils; |
| 40 |
import android.view.animation.DecelerateInterpolator; |
| 41 |
import android.view.animation.Interpolator; |
| 42 |
|
| 43 |
import java.lang.annotation.Retention; |
| 44 |
import java.lang.annotation.RetentionPolicy; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public class EdgeEffect { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@ChangeId |
| 71 |
@EnabledSince(targetSdkVersion = Build.VERSION_CODES.BASE) |
| 72 |
public static final long USE_STRETCH_EDGE_EFFECT_BY_DEFAULT = 171228096L; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final BlendMode DEFAULT_BLEND_MODE = BlendMode.SRC_ATOP; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private static final int TYPE_NONE = -1; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private static final int TYPE_GLOW = 0; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private static final int TYPE_STRETCH = 1; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private static final double VELOCITY_THRESHOLD = 0.01; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
private static final float LINEAR_VELOCITY_TAKE_OVER = 200f; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private static final double VALUE_THRESHOLD = 0.001; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
private static final double LINEAR_DISTANCE_TAKE_OVER = 8.0; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private static final double NATURAL_FREQUENCY = 24.657; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private static final double DAMPING_RATIO = 0.98; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private static final float ON_ABSORB_VELOCITY_ADJUSTMENT = 13f; |
| 137 |
|
| 138 |
|
| 139 |
@IntDef({TYPE_NONE, TYPE_GLOW, TYPE_STRETCH}) |
| 140 |
@Retention(RetentionPolicy.SOURCE) |
| 141 |
public @interface EdgeEffectType { |
| 142 |
} |
| 143 |
|
| 144 |
private static final float LINEAR_STRETCH_INTENSITY = 0.016f; |
| 145 |
|
| 146 |
private static final float EXP_STRETCH_INTENSITY = 0.016f; |
| 147 |
|
| 148 |
private static final float SCROLL_DIST_AFFECTED_BY_EXP_STRETCH = 0.33f; |
| 149 |
|
| 150 |
@SuppressWarnings("UnusedDeclaration") |
| 151 |
private static final String TAG = "EdgeEffect"; |
| 152 |
|
| 153 |
|
| 154 |
private static final int RECEDE_TIME = 600; |
| 155 |
|
| 156 |
|
| 157 |
private static final int PULL_TIME = 167; |
| 158 |
|
| 159 |
|
| 160 |
private static final int PULL_DECAY_TIME = 2000; |
| 161 |
|
| 162 |
private static final float MAX_ALPHA = 0.15f; |
| 163 |
private static final float GLOW_ALPHA_START = .09f; |
| 164 |
|
| 165 |
private static final float MAX_GLOW_SCALE = 2.f; |
| 166 |
|
| 167 |
private static final float PULL_GLOW_BEGIN = 0.f; |
| 168 |
|
| 169 |
|
| 170 |
private static final int MIN_VELOCITY = 100; |
| 171 |
|
| 172 |
private static final int MAX_VELOCITY = 10000; |
| 173 |
|
| 174 |
private static final float EPSILON = 0.001f; |
| 175 |
|
| 176 |
private static final double ANGLE = Math.PI / 6; |
| 177 |
private static final float SIN = (float) Math.sin(ANGLE); |
| 178 |
private static final float COS = (float) Math.cos(ANGLE); |
| 179 |
private static final float RADIUS_FACTOR = 0.6f; |
| 180 |
|
| 181 |
private float mGlowAlpha; |
| 182 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 183 |
private float mGlowScaleY; |
| 184 |
private float mDistance; |
| 185 |
private float mVelocity; |
| 186 |
|
| 187 |
private float mGlowAlphaStart; |
| 188 |
private float mGlowAlphaFinish; |
| 189 |
private float mGlowScaleYStart; |
| 190 |
private float mGlowScaleYFinish; |
| 191 |
|
| 192 |
private long mStartTime; |
| 193 |
private float mDuration; |
| 194 |
|
| 195 |
private final Interpolator mInterpolator = new DecelerateInterpolator(); |
| 196 |
|
| 197 |
private static final int STATE_IDLE = 0; |
| 198 |
private static final int STATE_PULL = 1; |
| 199 |
private static final int STATE_ABSORB = 2; |
| 200 |
private static final int STATE_RECEDE = 3; |
| 201 |
private static final int STATE_PULL_DECAY = 4; |
| 202 |
|
| 203 |
private static final float PULL_DISTANCE_ALPHA_GLOW_FACTOR = 0.8f; |
| 204 |
|
| 205 |
private static final int VELOCITY_GLOW_FACTOR = 6; |
| 206 |
|
| 207 |
private int mState = STATE_IDLE; |
| 208 |
|
| 209 |
private float mPullDistance; |
| 210 |
|
| 211 |
private final Rect mBounds = new Rect(); |
| 212 |
private float mWidth; |
| 213 |
private float mHeight; |
| 214 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123769450) |
| 215 |
private final Paint mPaint = new Paint(); |
| 216 |
private float mRadius; |
| 217 |
private float mBaseGlowScale; |
| 218 |
private float mDisplacement = 0.5f; |
| 219 |
private float mTargetDisplacement = 0.5f; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
private @EdgeEffectType int mEdgeEffectType = TYPE_GLOW; |
| 227 |
private Matrix mTmpMatrix = null; |
| 228 |
private float[] mTmpPoints = null; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public EdgeEffect(Context context) { |
| 235 |
this(context, null); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public EdgeEffect(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 244 |
final TypedArray a = context.obtainStyledAttributes( |
| 245 |
attrs, com.android.internal.R.styleable.EdgeEffect); |
| 246 |
final int themeColor = a.getColor( |
| 247 |
com.android.internal.R.styleable.EdgeEffect_colorEdgeEffect, 0xff666666); |
| 248 |
mEdgeEffectType = Compatibility.isChangeEnabled(USE_STRETCH_EDGE_EFFECT_BY_DEFAULT) |
| 249 |
? TYPE_STRETCH : TYPE_GLOW; |
| 250 |
a.recycle(); |
| 251 |
|
| 252 |
mPaint.setAntiAlias(true); |
| 253 |
mPaint.setColor((themeColor & 0xffffff) | 0x33000000); |
| 254 |
mPaint.setStyle(Paint.Style.FILL); |
| 255 |
mPaint.setBlendMode(DEFAULT_BLEND_MODE); |
| 256 |
} |
| 257 |
|
| 258 |
@EdgeEffectType |
| 259 |
private int getCurrentEdgeEffectBehavior() { |
| 260 |
if (!ValueAnimator.areAnimatorsEnabled()) { |
| 261 |
return TYPE_NONE; |
| 262 |
} else { |
| 263 |
return mEdgeEffectType; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public void setSize(int width, int height) { |
| 274 |
final float r = width * RADIUS_FACTOR / SIN; |
| 275 |
final float y = COS * r; |
| 276 |
final float h = r - y; |
| 277 |
final float or = height * RADIUS_FACTOR / SIN; |
| 278 |
final float oy = COS * or; |
| 279 |
final float oh = or - oy; |
| 280 |
|
| 281 |
mRadius = r; |
| 282 |
mBaseGlowScale = h > 0 ? Math.min(oh / h, 1.f) : 1.f; |
| 283 |
|
| 284 |
mBounds.set(mBounds.left, mBounds.top, width, (int) Math.min(height, h)); |
| 285 |
|
| 286 |
mWidth = width; |
| 287 |
mHeight = height; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public boolean isFinished() { |
| 298 |
return mState == STATE_IDLE; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public void finish() { |
| 306 |
mState = STATE_IDLE; |
| 307 |
mDistance = 0; |
| 308 |
mVelocity = 0; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public void onPull(float deltaDistance) { |
| 325 |
onPull(deltaDistance, 0.5f); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public void onPull(float deltaDistance, float displacement) { |
| 342 |
int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); |
| 343 |
if (edgeEffectBehavior == TYPE_NONE) { |
| 344 |
finish(); |
| 345 |
return; |
| 346 |
} |
| 347 |
final long now = AnimationUtils.currentAnimationTimeMillis(); |
| 348 |
mTargetDisplacement = displacement; |
| 349 |
if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration |
| 350 |
&& edgeEffectBehavior == TYPE_GLOW) { |
| 351 |
return; |
| 352 |
} |
| 353 |
if (mState != STATE_PULL) { |
| 354 |
if (edgeEffectBehavior == TYPE_STRETCH) { |
| 355 |
|
| 356 |
|
| 357 |
mPullDistance = mDistance; |
| 358 |
} else { |
| 359 |
mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY); |
| 360 |
} |
| 361 |
} |
| 362 |
mState = STATE_PULL; |
| 363 |
|
| 364 |
mStartTime = now; |
| 365 |
mDuration = PULL_TIME; |
| 366 |
|
| 367 |
mPullDistance += deltaDistance; |
| 368 |
if (edgeEffectBehavior == TYPE_STRETCH) { |
| 369 |
|
| 370 |
mPullDistance = Math.min(1f, mPullDistance); |
| 371 |
} |
| 372 |
mDistance = Math.max(0f, mPullDistance); |
| 373 |
mVelocity = 0; |
| 374 |
|
| 375 |
if (mPullDistance == 0) { |
| 376 |
mGlowScaleY = mGlowScaleYStart = 0; |
| 377 |
mGlowAlpha = mGlowAlphaStart = 0; |
| 378 |
} else { |
| 379 |
final float absdd = Math.abs(deltaDistance); |
| 380 |
mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA, |
| 381 |
mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR)); |
| 382 |
|
| 383 |
final float scale = (float) (Math.max(0, 1 - 1 / |
| 384 |
Math.sqrt(Math.abs(mPullDistance) * mBounds.height()) - 0.3d) / 0.7d); |
| 385 |
|
| 386 |
mGlowScaleY = mGlowScaleYStart = scale; |
| 387 |
} |
| 388 |
|
| 389 |
mGlowAlphaFinish = mGlowAlpha; |
| 390 |
mGlowScaleYFinish = mGlowScaleY; |
| 391 |
if (edgeEffectBehavior == TYPE_STRETCH && mDistance == 0) { |
| 392 |
mState = STATE_IDLE; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public float onPullDistance(float deltaDistance, float displacement) { |
| 426 |
int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); |
| 427 |
if (edgeEffectBehavior == TYPE_NONE) { |
| 428 |
return 0f; |
| 429 |
} |
| 430 |
float finalDistance = Math.max(0f, deltaDistance + mDistance); |
| 431 |
float delta = finalDistance - mDistance; |
| 432 |
if (delta == 0f && mDistance == 0f) { |
| 433 |
return 0f; |
| 434 |
} |
| 435 |
|
| 436 |
if (mState != STATE_PULL && mState != STATE_PULL_DECAY && edgeEffectBehavior == TYPE_GLOW) { |
| 437 |
|
| 438 |
mPullDistance = mDistance; |
| 439 |
mState = STATE_PULL; |
| 440 |
} |
| 441 |
onPull(delta, displacement); |
| 442 |
return delta; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public float getDistance() { |
| 456 |
return mDistance; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public void onRelease() { |
| 466 |
mPullDistance = 0; |
| 467 |
|
| 468 |
if (mState != STATE_PULL && mState != STATE_PULL_DECAY) { |
| 469 |
return; |
| 470 |
} |
| 471 |
|
| 472 |
mState = STATE_RECEDE; |
| 473 |
mGlowAlphaStart = mGlowAlpha; |
| 474 |
mGlowScaleYStart = mGlowScaleY; |
| 475 |
|
| 476 |
mGlowAlphaFinish = 0.f; |
| 477 |
mGlowScaleYFinish = 0.f; |
| 478 |
mVelocity = 0.f; |
| 479 |
|
| 480 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 481 |
mDuration = RECEDE_TIME; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public void onAbsorb(int velocity) { |
| 495 |
int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); |
| 496 |
if (edgeEffectBehavior == TYPE_STRETCH) { |
| 497 |
mState = STATE_RECEDE; |
| 498 |
mVelocity = velocity * ON_ABSORB_VELOCITY_ADJUSTMENT; |
| 499 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 500 |
} else if (edgeEffectBehavior == TYPE_GLOW) { |
| 501 |
mState = STATE_ABSORB; |
| 502 |
mVelocity = 0; |
| 503 |
velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY); |
| 504 |
|
| 505 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 506 |
mDuration = 0.15f + (velocity * 0.02f); |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
mGlowAlphaStart = GLOW_ALPHA_START; |
| 511 |
mGlowScaleYStart = Math.max(mGlowScaleY, 0.f); |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, |
| 518 |
1.f); |
| 519 |
|
| 520 |
mGlowAlphaFinish = Math.max( |
| 521 |
mGlowAlphaStart, |
| 522 |
Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA)); |
| 523 |
mTargetDisplacement = 0.5f; |
| 524 |
} else { |
| 525 |
finish(); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public void setColor(@ColorInt int color) { |
| 535 |
mPaint.setColor(color); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
public void setBlendMode(@Nullable BlendMode blendmode) { |
| 551 |
mPaint.setBlendMode(blendmode); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
@ColorInt |
| 559 |
public int getColor() { |
| 560 |
return mPaint.getColor(); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
@Nullable |
| 572 |
public BlendMode getBlendMode() { |
| 573 |
return mPaint.getBlendMode(); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public boolean draw(Canvas canvas) { |
| 588 |
int edgeEffectBehavior = getCurrentEdgeEffectBehavior(); |
| 589 |
if (edgeEffectBehavior == TYPE_GLOW) { |
| 590 |
update(); |
| 591 |
final int count = canvas.save(); |
| 592 |
|
| 593 |
final float centerX = mBounds.centerX(); |
| 594 |
final float centerY = mBounds.height() - mRadius; |
| 595 |
|
| 596 |
canvas.scale(1.f, Math.min(mGlowScaleY, 1.f) * mBaseGlowScale, centerX, 0); |
| 597 |
|
| 598 |
final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f; |
| 599 |
float translateX = mBounds.width() * displacement / 2; |
| 600 |
|
| 601 |
canvas.clipRect(mBounds); |
| 602 |
canvas.translate(translateX, 0); |
| 603 |
mPaint.setAlpha((int) (0xff * mGlowAlpha)); |
| 604 |
canvas.drawCircle(centerX, centerY, mRadius, mPaint); |
| 605 |
canvas.restoreToCount(count); |
| 606 |
} else if (edgeEffectBehavior == TYPE_STRETCH && canvas instanceof RecordingCanvas) { |
| 607 |
if (mState == STATE_RECEDE) { |
| 608 |
updateSpring(); |
| 609 |
} |
| 610 |
if (mDistance != 0f) { |
| 611 |
RecordingCanvas recordingCanvas = (RecordingCanvas) canvas; |
| 612 |
if (mTmpMatrix == null) { |
| 613 |
mTmpMatrix = new Matrix(); |
| 614 |
mTmpPoints = new float[12]; |
| 615 |
} |
| 616 |
|
| 617 |
recordingCanvas.getMatrix(mTmpMatrix); |
| 618 |
|
| 619 |
mTmpPoints[0] = 0; |
| 620 |
mTmpPoints[1] = 0; |
| 621 |
mTmpPoints[2] = mWidth; |
| 622 |
mTmpPoints[3] = 0; |
| 623 |
mTmpPoints[4] = mWidth; |
| 624 |
mTmpPoints[5] = mHeight; |
| 625 |
mTmpPoints[6] = 0; |
| 626 |
mTmpPoints[7] = mHeight; |
| 627 |
mTmpPoints[8] = mWidth * mDisplacement; |
| 628 |
mTmpPoints[9] = 0; |
| 629 |
mTmpPoints[10] = mWidth * mDisplacement; |
| 630 |
mTmpPoints[11] = mHeight * mDistance; |
| 631 |
mTmpMatrix.mapPoints(mTmpPoints); |
| 632 |
|
| 633 |
RenderNode renderNode = recordingCanvas.mNode; |
| 634 |
|
| 635 |
float left = renderNode.getLeft() |
| 636 |
+ min(mTmpPoints[0], mTmpPoints[2], mTmpPoints[4], mTmpPoints[6]); |
| 637 |
float top = renderNode.getTop() |
| 638 |
+ min(mTmpPoints[1], mTmpPoints[3], mTmpPoints[5], mTmpPoints[7]); |
| 639 |
float right = renderNode.getLeft() |
| 640 |
+ max(mTmpPoints[0], mTmpPoints[2], mTmpPoints[4], mTmpPoints[6]); |
| 641 |
float bottom = renderNode.getTop() |
| 642 |
+ max(mTmpPoints[1], mTmpPoints[3], mTmpPoints[5], mTmpPoints[7]); |
| 643 |
|
| 644 |
float x = mTmpPoints[10] - mTmpPoints[8]; |
| 645 |
float width = right - left; |
| 646 |
float vecX = dampStretchVector(Math.max(-1f, Math.min(1f, x / width))); |
| 647 |
|
| 648 |
float y = mTmpPoints[11] - mTmpPoints[9]; |
| 649 |
float height = bottom - top; |
| 650 |
float vecY = dampStretchVector(Math.max(-1f, Math.min(1f, y / height))); |
| 651 |
|
| 652 |
boolean hasValidVectors = Float.isFinite(vecX) && Float.isFinite(vecY); |
| 653 |
if (right > left && bottom > top && mWidth > 0 && mHeight > 0 && hasValidVectors) { |
| 654 |
renderNode.stretch( |
| 655 |
vecX, |
| 656 |
vecY, |
| 657 |
mWidth, |
| 658 |
mHeight |
| 659 |
); |
| 660 |
} |
| 661 |
} |
| 662 |
} else { |
| 663 |
|
| 664 |
|
| 665 |
mState = STATE_IDLE; |
| 666 |
mDistance = 0; |
| 667 |
mVelocity = 0; |
| 668 |
} |
| 669 |
|
| 670 |
boolean oneLastFrame = false; |
| 671 |
if (mState == STATE_RECEDE && mDistance == 0 && mVelocity == 0) { |
| 672 |
mState = STATE_IDLE; |
| 673 |
oneLastFrame = true; |
| 674 |
} |
| 675 |
|
| 676 |
return mState != STATE_IDLE || oneLastFrame; |
| 677 |
} |
| 678 |
|
| 679 |
private float min(float f1, float f2, float f3, float f4) { |
| 680 |
float min = Math.min(f1, f2); |
| 681 |
min = Math.min(min, f3); |
| 682 |
return Math.min(min, f4); |
| 683 |
} |
| 684 |
|
| 685 |
private float max(float f1, float f2, float f3, float f4) { |
| 686 |
float max = Math.max(f1, f2); |
| 687 |
max = Math.max(max, f3); |
| 688 |
return Math.max(max, f4); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public int getMaxHeight() { |
| 697 |
return (int) mHeight; |
| 698 |
} |
| 699 |
|
| 700 |
private void update() { |
| 701 |
final long time = AnimationUtils.currentAnimationTimeMillis(); |
| 702 |
final float t = Math.min((time - mStartTime) / mDuration, 1.f); |
| 703 |
|
| 704 |
final float interp = mInterpolator.getInterpolation(t); |
| 705 |
|
| 706 |
mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp; |
| 707 |
mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp; |
| 708 |
if (mState != STATE_PULL) { |
| 709 |
mDistance = calculateDistanceFromGlowValues(mGlowScaleY, mGlowAlpha); |
| 710 |
} |
| 711 |
mDisplacement = (mDisplacement + mTargetDisplacement) / 2; |
| 712 |
|
| 713 |
if (t >= 1.f - EPSILON) { |
| 714 |
switch (mState) { |
| 715 |
case STATE_ABSORB: |
| 716 |
mState = STATE_RECEDE; |
| 717 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 718 |
mDuration = RECEDE_TIME; |
| 719 |
|
| 720 |
mGlowAlphaStart = mGlowAlpha; |
| 721 |
mGlowScaleYStart = mGlowScaleY; |
| 722 |
|
| 723 |
|
| 724 |
mGlowAlphaFinish = 0.f; |
| 725 |
mGlowScaleYFinish = 0.f; |
| 726 |
break; |
| 727 |
case STATE_PULL: |
| 728 |
mState = STATE_PULL_DECAY; |
| 729 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 730 |
mDuration = PULL_DECAY_TIME; |
| 731 |
|
| 732 |
mGlowAlphaStart = mGlowAlpha; |
| 733 |
mGlowScaleYStart = mGlowScaleY; |
| 734 |
|
| 735 |
|
| 736 |
mGlowAlphaFinish = 0.f; |
| 737 |
mGlowScaleYFinish = 0.f; |
| 738 |
break; |
| 739 |
case STATE_PULL_DECAY: |
| 740 |
mState = STATE_RECEDE; |
| 741 |
break; |
| 742 |
case STATE_RECEDE: |
| 743 |
mState = STATE_IDLE; |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
private void updateSpring() { |
| 750 |
final long time = AnimationUtils.currentAnimationTimeMillis(); |
| 751 |
final float deltaT = (time - mStartTime) / 1000f; |
| 752 |
if (deltaT < 0.001f) { |
| 753 |
return; |
| 754 |
} |
| 755 |
mStartTime = time; |
| 756 |
|
| 757 |
if (Math.abs(mVelocity) <= LINEAR_VELOCITY_TAKE_OVER |
| 758 |
&& Math.abs(mDistance * mHeight) < LINEAR_DISTANCE_TAKE_OVER |
| 759 |
&& Math.signum(mVelocity) == -Math.signum(mDistance) |
| 760 |
) { |
| 761 |
|
| 762 |
|
| 763 |
mVelocity = Math.signum(mVelocity) * LINEAR_VELOCITY_TAKE_OVER; |
| 764 |
|
| 765 |
float targetDistance = mDistance + (mVelocity * deltaT / mHeight); |
| 766 |
if (Math.signum(targetDistance) != Math.signum(mDistance)) { |
| 767 |
|
| 768 |
mDistance = 0; |
| 769 |
mVelocity = 0; |
| 770 |
} else { |
| 771 |
mDistance = targetDistance; |
| 772 |
} |
| 773 |
return; |
| 774 |
} |
| 775 |
final double mDampedFreq = NATURAL_FREQUENCY * Math.sqrt(1 - DAMPING_RATIO * DAMPING_RATIO); |
| 776 |
|
| 777 |
|
| 778 |
double cosCoeff = mDistance * mHeight; |
| 779 |
double sinCoeff = (1 / mDampedFreq) * (DAMPING_RATIO * NATURAL_FREQUENCY |
| 780 |
* mDistance * mHeight + mVelocity); |
| 781 |
double distance = Math.pow(Math.E, -DAMPING_RATIO * NATURAL_FREQUENCY * deltaT) |
| 782 |
* (cosCoeff * Math.cos(mDampedFreq * deltaT) |
| 783 |
+ sinCoeff * Math.sin(mDampedFreq * deltaT)); |
| 784 |
double velocity = distance * (-NATURAL_FREQUENCY) * DAMPING_RATIO |
| 785 |
+ Math.pow(Math.E, -DAMPING_RATIO * NATURAL_FREQUENCY * deltaT) |
| 786 |
* (-mDampedFreq * cosCoeff * Math.sin(mDampedFreq * deltaT) |
| 787 |
+ mDampedFreq * sinCoeff * Math.cos(mDampedFreq * deltaT)); |
| 788 |
mDistance = (float) distance / mHeight; |
| 789 |
mVelocity = (float) velocity; |
| 790 |
if (mDistance > 1f) { |
| 791 |
mDistance = 1f; |
| 792 |
mVelocity = 0f; |
| 793 |
} |
| 794 |
if (isAtEquilibrium()) { |
| 795 |
mDistance = 0; |
| 796 |
mVelocity = 0; |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
private float calculateDistanceFromGlowValues(float scale, float alpha) { |
| 804 |
if (scale >= 1f) { |
| 805 |
|
| 806 |
|
| 807 |
return 1f; |
| 808 |
} |
| 809 |
if (scale > 0f) { |
| 810 |
float v = 1f / 0.7f / (mGlowScaleY - 1f); |
| 811 |
return v * v / mBounds.height(); |
| 812 |
} |
| 813 |
return alpha / PULL_DISTANCE_ALPHA_GLOW_FACTOR; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
private boolean isAtEquilibrium() { |
| 821 |
double displacement = mDistance * mHeight; |
| 822 |
double velocity = mVelocity; |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
return displacement < 0 || (Math.abs(velocity) < VELOCITY_THRESHOLD |
| 828 |
&& displacement < VALUE_THRESHOLD); |
| 829 |
} |
| 830 |
|
| 831 |
private float dampStretchVector(float normalizedVec) { |
| 832 |
float sign = normalizedVec > 0 ? 1f : -1f; |
| 833 |
float overscroll = Math.abs(normalizedVec); |
| 834 |
float linearIntensity = LINEAR_STRETCH_INTENSITY * overscroll; |
| 835 |
double scalar = Math.E / SCROLL_DIST_AFFECTED_BY_EXP_STRETCH; |
| 836 |
double expIntensity = EXP_STRETCH_INTENSITY * (1 - Math.exp(-overscroll * scalar)); |
| 837 |
return sign * (float) (linearIntensity + expIntensity); |
| 838 |
} |
| 839 |
} |