| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.Context; |
| 21 |
import android.hardware.SensorManager; |
| 22 |
import android.os.Build; |
| 23 |
import android.view.ViewConfiguration; |
| 24 |
import android.view.animation.AnimationUtils; |
| 25 |
import android.view.animation.Interpolator; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public class Scroller { |
| 64 |
@UnsupportedAppUsage |
| 65 |
private final Interpolator mInterpolator; |
| 66 |
|
| 67 |
private int mMode; |
| 68 |
|
| 69 |
private int mStartX; |
| 70 |
private int mStartY; |
| 71 |
private int mFinalX; |
| 72 |
private int mFinalY; |
| 73 |
|
| 74 |
private int mMinX; |
| 75 |
private int mMaxX; |
| 76 |
private int mMinY; |
| 77 |
private int mMaxY; |
| 78 |
|
| 79 |
private int mCurrX; |
| 80 |
private int mCurrY; |
| 81 |
private long mStartTime; |
| 82 |
@UnsupportedAppUsage |
| 83 |
private int mDuration; |
| 84 |
private float mDurationReciprocal; |
| 85 |
private float mDeltaX; |
| 86 |
private float mDeltaY; |
| 87 |
private boolean mFinished; |
| 88 |
private boolean mFlywheel; |
| 89 |
|
| 90 |
private float mVelocity; |
| 91 |
private float mCurrVelocity; |
| 92 |
private int mDistance; |
| 93 |
|
| 94 |
private float mFlingFriction = ViewConfiguration.getScrollFriction(); |
| 95 |
|
| 96 |
private static final int DEFAULT_DURATION = 250; |
| 97 |
private static final int SCROLL_MODE = 0; |
| 98 |
private static final int FLING_MODE = 1; |
| 99 |
|
| 100 |
@UnsupportedAppUsage |
| 101 |
private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9)); |
| 102 |
@UnsupportedAppUsage |
| 103 |
private static final float INFLEXION = 0.35f; |
| 104 |
private static final float START_TENSION = 0.5f; |
| 105 |
private static final float END_TENSION = 1.0f; |
| 106 |
private static final float P1 = START_TENSION * INFLEXION; |
| 107 |
private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION); |
| 108 |
|
| 109 |
private static final int NB_SAMPLES = 100; |
| 110 |
private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1]; |
| 111 |
private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1]; |
| 112 |
|
| 113 |
@UnsupportedAppUsage |
| 114 |
private float mDeceleration; |
| 115 |
private final float mPpi; |
| 116 |
|
| 117 |
|
| 118 |
@UnsupportedAppUsage |
| 119 |
private float mPhysicalCoeff; |
| 120 |
|
| 121 |
static { |
| 122 |
float x_min = 0.0f; |
| 123 |
float y_min = 0.0f; |
| 124 |
for (int i = 0; i < NB_SAMPLES; i++) { |
| 125 |
final float alpha = (float) i / NB_SAMPLES; |
| 126 |
|
| 127 |
float x_max = 1.0f; |
| 128 |
float x, tx, coef; |
| 129 |
while (true) { |
| 130 |
x = x_min + (x_max - x_min) / 2.0f; |
| 131 |
coef = 3.0f * x * (1.0f - x); |
| 132 |
tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x; |
| 133 |
if (Math.abs(tx - alpha) < 1E-5) break; |
| 134 |
if (tx > alpha) x_max = x; |
| 135 |
else x_min = x; |
| 136 |
} |
| 137 |
SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x; |
| 138 |
|
| 139 |
float y_max = 1.0f; |
| 140 |
float y, dy; |
| 141 |
while (true) { |
| 142 |
y = y_min + (y_max - y_min) / 2.0f; |
| 143 |
coef = 3.0f * y * (1.0f - y); |
| 144 |
dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y; |
| 145 |
if (Math.abs(dy - alpha) < 1E-5) break; |
| 146 |
if (dy > alpha) y_max = y; |
| 147 |
else y_min = y; |
| 148 |
} |
| 149 |
SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y; |
| 150 |
} |
| 151 |
SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public Scroller(Context context) { |
| 158 |
this(context, null); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public Scroller(Context context, Interpolator interpolator) { |
| 167 |
this(context, interpolator, |
| 168 |
context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public Scroller(Context context, Interpolator interpolator, boolean flywheel) { |
| 177 |
mFinished = true; |
| 178 |
if (interpolator == null) { |
| 179 |
mInterpolator = new ViscousFluidInterpolator(); |
| 180 |
} else { |
| 181 |
mInterpolator = interpolator; |
| 182 |
} |
| 183 |
mPpi = context.getResources().getDisplayMetrics().density * 160.0f; |
| 184 |
mDeceleration = computeDeceleration(ViewConfiguration.getScrollFriction()); |
| 185 |
mFlywheel = flywheel; |
| 186 |
|
| 187 |
mPhysicalCoeff = computeDeceleration(0.84f); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public final void setFriction(float friction) { |
| 198 |
mDeceleration = computeDeceleration(friction); |
| 199 |
mFlingFriction = friction; |
| 200 |
} |
| 201 |
|
| 202 |
private float computeDeceleration(float friction) { |
| 203 |
return SensorManager.GRAVITY_EARTH |
| 204 |
* 39.37f |
| 205 |
* mPpi |
| 206 |
* friction; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public final boolean isFinished() { |
| 216 |
return mFinished; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public final void forceFinished(boolean finished) { |
| 225 |
mFinished = finished; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public final int getDuration() { |
| 234 |
return mDuration; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public final int getCurrX() { |
| 243 |
return mCurrX; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public final int getCurrY() { |
| 252 |
return mCurrY; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public float getCurrVelocity() { |
| 262 |
return mMode == FLING_MODE ? |
| 263 |
mCurrVelocity : mVelocity - mDeceleration * timePassed() / 2000.0f; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public final int getStartX() { |
| 272 |
return mStartX; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public final int getStartY() { |
| 281 |
return mStartY; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public final int getFinalX() { |
| 290 |
return mFinalX; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public final int getFinalY() { |
| 299 |
return mFinalY; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public boolean computeScrollOffset() { |
| 307 |
if (mFinished) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime); |
| 312 |
|
| 313 |
if (timePassed < mDuration) { |
| 314 |
switch (mMode) { |
| 315 |
case SCROLL_MODE: |
| 316 |
final float x = mInterpolator.getInterpolation(timePassed * mDurationReciprocal); |
| 317 |
mCurrX = mStartX + Math.round(x * mDeltaX); |
| 318 |
mCurrY = mStartY + Math.round(x * mDeltaY); |
| 319 |
break; |
| 320 |
case FLING_MODE: |
| 321 |
final float t = (float) timePassed / mDuration; |
| 322 |
final int index = (int) (NB_SAMPLES * t); |
| 323 |
float distanceCoef = 1.f; |
| 324 |
float velocityCoef = 0.f; |
| 325 |
if (index < NB_SAMPLES) { |
| 326 |
final float t_inf = (float) index / NB_SAMPLES; |
| 327 |
final float t_sup = (float) (index + 1) / NB_SAMPLES; |
| 328 |
final float d_inf = SPLINE_POSITION[index]; |
| 329 |
final float d_sup = SPLINE_POSITION[index + 1]; |
| 330 |
velocityCoef = (d_sup - d_inf) / (t_sup - t_inf); |
| 331 |
distanceCoef = d_inf + (t - t_inf) * velocityCoef; |
| 332 |
} |
| 333 |
|
| 334 |
mCurrVelocity = velocityCoef * mDistance / mDuration * 1000.0f; |
| 335 |
|
| 336 |
mCurrX = mStartX + Math.round(distanceCoef * (mFinalX - mStartX)); |
| 337 |
|
| 338 |
mCurrX = Math.min(mCurrX, mMaxX); |
| 339 |
mCurrX = Math.max(mCurrX, mMinX); |
| 340 |
|
| 341 |
mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY)); |
| 342 |
|
| 343 |
mCurrY = Math.min(mCurrY, mMaxY); |
| 344 |
mCurrY = Math.max(mCurrY, mMinY); |
| 345 |
|
| 346 |
if (mCurrX == mFinalX && mCurrY == mFinalY) { |
| 347 |
mFinished = true; |
| 348 |
} |
| 349 |
|
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
else { |
| 354 |
mCurrX = mFinalX; |
| 355 |
mCurrY = mFinalY; |
| 356 |
mFinished = true; |
| 357 |
} |
| 358 |
return true; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public void startScroll(int startX, int startY, int dx, int dy) { |
| 376 |
startScroll(startX, startY, dx, dy, DEFAULT_DURATION); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public void startScroll(int startX, int startY, int dx, int dy, int duration) { |
| 394 |
mMode = SCROLL_MODE; |
| 395 |
mFinished = false; |
| 396 |
mDuration = duration; |
| 397 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 398 |
mStartX = startX; |
| 399 |
mStartY = startY; |
| 400 |
mFinalX = startX + dx; |
| 401 |
mFinalY = startY + dy; |
| 402 |
mDeltaX = dx; |
| 403 |
mDeltaY = dy; |
| 404 |
mDurationReciprocal = 1.0f / (float) mDuration; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public void fling(int startX, int startY, int velocityX, int velocityY, |
| 427 |
int minX, int maxX, int minY, int maxY) { |
| 428 |
|
| 429 |
if (mFlywheel && !mFinished) { |
| 430 |
float oldVel = getCurrVelocity(); |
| 431 |
|
| 432 |
float dx = (float) (mFinalX - mStartX); |
| 433 |
float dy = (float) (mFinalY - mStartY); |
| 434 |
float hyp = (float) Math.hypot(dx, dy); |
| 435 |
|
| 436 |
float ndx = dx / hyp; |
| 437 |
float ndy = dy / hyp; |
| 438 |
|
| 439 |
float oldVelocityX = ndx * oldVel; |
| 440 |
float oldVelocityY = ndy * oldVel; |
| 441 |
if (Math.signum(velocityX) == Math.signum(oldVelocityX) && |
| 442 |
Math.signum(velocityY) == Math.signum(oldVelocityY)) { |
| 443 |
velocityX += oldVelocityX; |
| 444 |
velocityY += oldVelocityY; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
mMode = FLING_MODE; |
| 449 |
mFinished = false; |
| 450 |
|
| 451 |
float velocity = (float) Math.hypot(velocityX, velocityY); |
| 452 |
|
| 453 |
mVelocity = velocity; |
| 454 |
mDuration = getSplineFlingDuration(velocity); |
| 455 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 456 |
mStartX = startX; |
| 457 |
mStartY = startY; |
| 458 |
|
| 459 |
float coeffX = velocity == 0 ? 1.0f : velocityX / velocity; |
| 460 |
float coeffY = velocity == 0 ? 1.0f : velocityY / velocity; |
| 461 |
|
| 462 |
double totalDistance = getSplineFlingDistance(velocity); |
| 463 |
mDistance = (int) (totalDistance * Math.signum(velocity)); |
| 464 |
|
| 465 |
mMinX = minX; |
| 466 |
mMaxX = maxX; |
| 467 |
mMinY = minY; |
| 468 |
mMaxY = maxY; |
| 469 |
|
| 470 |
mFinalX = startX + (int) Math.round(totalDistance * coeffX); |
| 471 |
|
| 472 |
mFinalX = Math.min(mFinalX, mMaxX); |
| 473 |
mFinalX = Math.max(mFinalX, mMinX); |
| 474 |
|
| 475 |
mFinalY = startY + (int) Math.round(totalDistance * coeffY); |
| 476 |
|
| 477 |
mFinalY = Math.min(mFinalY, mMaxY); |
| 478 |
mFinalY = Math.max(mFinalY, mMinY); |
| 479 |
} |
| 480 |
|
| 481 |
private double getSplineDeceleration(float velocity) { |
| 482 |
return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff)); |
| 483 |
} |
| 484 |
|
| 485 |
private int getSplineFlingDuration(float velocity) { |
| 486 |
final double l = getSplineDeceleration(velocity); |
| 487 |
final double decelMinusOne = DECELERATION_RATE - 1.0; |
| 488 |
return (int) (1000.0 * Math.exp(l / decelMinusOne)); |
| 489 |
} |
| 490 |
|
| 491 |
private double getSplineFlingDistance(float velocity) { |
| 492 |
final double l = getSplineDeceleration(velocity); |
| 493 |
final double decelMinusOne = DECELERATION_RATE - 1.0; |
| 494 |
return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public void abortAnimation() { |
| 505 |
mCurrX = mFinalX; |
| 506 |
mCurrY = mFinalY; |
| 507 |
mFinished = true; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
public void extendDuration(int extend) { |
| 519 |
int passed = timePassed(); |
| 520 |
mDuration = passed + extend; |
| 521 |
mDurationReciprocal = 1.0f / mDuration; |
| 522 |
mFinished = false; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public int timePassed() { |
| 531 |
return (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public void setFinalX(int newX) { |
| 542 |
mFinalX = newX; |
| 543 |
mDeltaX = mFinalX - mStartX; |
| 544 |
mFinished = false; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
public void setFinalY(int newY) { |
| 555 |
mFinalY = newY; |
| 556 |
mDeltaY = mFinalY - mStartY; |
| 557 |
mFinished = false; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public boolean isScrollingInDirection(float xvel, float yvel) { |
| 564 |
return !mFinished && Math.signum(xvel) == Math.signum(mFinalX - mStartX) && |
| 565 |
Math.signum(yvel) == Math.signum(mFinalY - mStartY); |
| 566 |
} |
| 567 |
|
| 568 |
static class ViscousFluidInterpolator implements Interpolator { |
| 569 |
|
| 570 |
private static final float VISCOUS_FLUID_SCALE = 8.0f; |
| 571 |
|
| 572 |
private static final float VISCOUS_FLUID_NORMALIZE; |
| 573 |
private static final float VISCOUS_FLUID_OFFSET; |
| 574 |
|
| 575 |
static { |
| 576 |
|
| 577 |
|
| 578 |
VISCOUS_FLUID_NORMALIZE = 1.0f / viscousFluid(1.0f); |
| 579 |
|
| 580 |
VISCOUS_FLUID_OFFSET = 1.0f - VISCOUS_FLUID_NORMALIZE * viscousFluid(1.0f); |
| 581 |
} |
| 582 |
|
| 583 |
private static float viscousFluid(float x) { |
| 584 |
x *= VISCOUS_FLUID_SCALE; |
| 585 |
if (x < 1.0f) { |
| 586 |
x -= (1.0f - (float)Math.exp(-x)); |
| 587 |
} else { |
| 588 |
float start = 0.36787944117f; |
| 589 |
x = 1.0f - (float)Math.exp(1.0f - x); |
| 590 |
x = start + x * (1.0f - start); |
| 591 |
} |
| 592 |
return x; |
| 593 |
} |
| 594 |
|
| 595 |
@Override |
| 596 |
public float getInterpolation(float input) { |
| 597 |
final float interpolated = VISCOUS_FLUID_NORMALIZE * viscousFluid(input); |
| 598 |
if (interpolated > 0) { |
| 599 |
return interpolated + VISCOUS_FLUID_OFFSET; |
| 600 |
} |
| 601 |
return interpolated; |
| 602 |
} |
| 603 |
} |
| 604 |
} |