| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.widget; |
| 18 |
|
| 19 |
import android.content.res.Resources; |
| 20 |
import android.os.SystemClock; |
| 21 |
import android.util.DisplayMetrics; |
| 22 |
import android.view.MotionEvent; |
| 23 |
import android.view.View; |
| 24 |
import android.view.ViewConfiguration; |
| 25 |
import android.view.animation.AccelerateInterpolator; |
| 26 |
import android.view.animation.AnimationUtils; |
| 27 |
import android.view.animation.Interpolator; |
| 28 |
import android.widget.AbsListView; |
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public abstract class AutoScrollHelper implements View.OnTouchListener { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static final float RELATIVE_UNSPECIFIED = 0; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final float NO_MAX = Float.MAX_VALUE; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static final float NO_MIN = 0; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static final int EDGE_TYPE_INSIDE = 0; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static final int EDGE_TYPE_INSIDE_EXTEND = 1; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static final int EDGE_TYPE_OUTSIDE = 2; |
| 131 |
|
| 132 |
private static final int HORIZONTAL = 0; |
| 133 |
private static final int VERTICAL = 1; |
| 134 |
|
| 135 |
|
| 136 |
private final ClampedScroller mScroller = new ClampedScroller(); |
| 137 |
|
| 138 |
|
| 139 |
private final Interpolator mEdgeInterpolator = new AccelerateInterpolator(); |
| 140 |
|
| 141 |
|
| 142 |
private final View mTarget; |
| 143 |
|
| 144 |
|
| 145 |
private Runnable mRunnable; |
| 146 |
|
| 147 |
|
| 148 |
private float[] mRelativeEdges = new float[] { RELATIVE_UNSPECIFIED, RELATIVE_UNSPECIFIED }; |
| 149 |
|
| 150 |
|
| 151 |
private float[] mMaximumEdges = new float[] { NO_MAX, NO_MAX }; |
| 152 |
|
| 153 |
|
| 154 |
private int mEdgeType; |
| 155 |
|
| 156 |
|
| 157 |
private int mActivationDelay; |
| 158 |
|
| 159 |
|
| 160 |
private float[] mRelativeVelocity = new float[] { RELATIVE_UNSPECIFIED, RELATIVE_UNSPECIFIED }; |
| 161 |
|
| 162 |
|
| 163 |
private float[] mMinimumVelocity = new float[] { NO_MIN, NO_MIN }; |
| 164 |
|
| 165 |
|
| 166 |
private float[] mMaximumVelocity = new float[] { NO_MAX, NO_MAX }; |
| 167 |
|
| 168 |
|
| 169 |
private boolean mAlreadyDelayed; |
| 170 |
|
| 171 |
|
| 172 |
private boolean mNeedsReset; |
| 173 |
|
| 174 |
|
| 175 |
private boolean mNeedsCancel; |
| 176 |
|
| 177 |
|
| 178 |
private boolean mAnimating; |
| 179 |
|
| 180 |
|
| 181 |
private boolean mEnabled; |
| 182 |
|
| 183 |
|
| 184 |
private boolean mExclusive; |
| 185 |
|
| 186 |
|
| 187 |
private static final int DEFAULT_EDGE_TYPE = EDGE_TYPE_INSIDE_EXTEND; |
| 188 |
private static final int DEFAULT_MINIMUM_VELOCITY_DIPS = 315; |
| 189 |
private static final int DEFAULT_MAXIMUM_VELOCITY_DIPS = 1575; |
| 190 |
private static final float DEFAULT_MAXIMUM_EDGE = NO_MAX; |
| 191 |
private static final float DEFAULT_RELATIVE_EDGE = 0.2f; |
| 192 |
private static final float DEFAULT_RELATIVE_VELOCITY = 1f; |
| 193 |
private static final int DEFAULT_ACTIVATION_DELAY = ViewConfiguration.getTapTimeout(); |
| 194 |
private static final int DEFAULT_RAMP_UP_DURATION = 500; |
| 195 |
private static final int DEFAULT_RAMP_DOWN_DURATION = 500; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public AutoScrollHelper(View target) { |
| 209 |
mTarget = target; |
| 210 |
|
| 211 |
final DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); |
| 212 |
final int maxVelocity = (int) (DEFAULT_MAXIMUM_VELOCITY_DIPS * metrics.density + 0.5f); |
| 213 |
final int minVelocity = (int) (DEFAULT_MINIMUM_VELOCITY_DIPS * metrics.density + 0.5f); |
| 214 |
setMaximumVelocity(maxVelocity, maxVelocity); |
| 215 |
setMinimumVelocity(minVelocity, minVelocity); |
| 216 |
|
| 217 |
setEdgeType(DEFAULT_EDGE_TYPE); |
| 218 |
setMaximumEdges(DEFAULT_MAXIMUM_EDGE, DEFAULT_MAXIMUM_EDGE); |
| 219 |
setRelativeEdges(DEFAULT_RELATIVE_EDGE, DEFAULT_RELATIVE_EDGE); |
| 220 |
setRelativeVelocity(DEFAULT_RELATIVE_VELOCITY, DEFAULT_RELATIVE_VELOCITY); |
| 221 |
setActivationDelay(DEFAULT_ACTIVATION_DELAY); |
| 222 |
setRampUpDuration(DEFAULT_RAMP_UP_DURATION); |
| 223 |
setRampDownDuration(DEFAULT_RAMP_DOWN_DURATION); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public AutoScrollHelper setEnabled(boolean enabled) { |
| 234 |
if (mEnabled && !enabled) { |
| 235 |
requestStop(); |
| 236 |
} |
| 237 |
|
| 238 |
mEnabled = enabled; |
| 239 |
return this; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public boolean isEnabled() { |
| 246 |
return mEnabled; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public AutoScrollHelper setExclusive(boolean exclusive) { |
| 262 |
mExclusive = exclusive; |
| 263 |
return this; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public boolean isExclusive() { |
| 275 |
return mExclusive; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public AutoScrollHelper setMaximumVelocity(float horizontalMax, float verticalMax) { |
| 293 |
mMaximumVelocity[HORIZONTAL] = horizontalMax / 1000f; |
| 294 |
mMaximumVelocity[VERTICAL] = verticalMax / 1000f; |
| 295 |
return this; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public AutoScrollHelper setMinimumVelocity(float horizontalMin, float verticalMin) { |
| 311 |
mMinimumVelocity[HORIZONTAL] = horizontalMin / 1000f; |
| 312 |
mMinimumVelocity[VERTICAL] = verticalMin / 1000f; |
| 313 |
return this; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public AutoScrollHelper setRelativeVelocity(float horizontal, float vertical) { |
| 332 |
mRelativeVelocity[HORIZONTAL] = horizontal / 1000f; |
| 333 |
mRelativeVelocity[VERTICAL] = vertical / 1000f; |
| 334 |
return this; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public AutoScrollHelper setEdgeType(int type) { |
| 353 |
mEdgeType = type; |
| 354 |
return this; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public AutoScrollHelper setRelativeEdges(float horizontal, float vertical) { |
| 372 |
mRelativeEdges[HORIZONTAL] = horizontal; |
| 373 |
mRelativeEdges[VERTICAL] = vertical; |
| 374 |
return this; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public AutoScrollHelper setMaximumEdges(float horizontalMax, float verticalMax) { |
| 394 |
mMaximumEdges[HORIZONTAL] = horizontalMax; |
| 395 |
mMaximumEdges[VERTICAL] = verticalMax; |
| 396 |
return this; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public AutoScrollHelper setActivationDelay(int delayMillis) { |
| 411 |
mActivationDelay = delayMillis; |
| 412 |
return this; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public AutoScrollHelper setRampUpDuration(int durationMillis) { |
| 426 |
mScroller.setRampUpDuration(durationMillis); |
| 427 |
return this; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public AutoScrollHelper setRampDownDuration(int durationMillis) { |
| 441 |
mScroller.setRampDownDuration(durationMillis); |
| 442 |
return this; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
@Override |
| 454 |
public boolean onTouch(View v, MotionEvent event) { |
| 455 |
if (!mEnabled) { |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
final int action = event.getActionMasked(); |
| 460 |
switch (action) { |
| 461 |
case MotionEvent.ACTION_DOWN: |
| 462 |
mNeedsCancel = true; |
| 463 |
mAlreadyDelayed = false; |
| 464 |
|
| 465 |
case MotionEvent.ACTION_MOVE: |
| 466 |
final float xTargetVelocity = computeTargetVelocity( |
| 467 |
HORIZONTAL, event.getX(), v.getWidth(), mTarget.getWidth()); |
| 468 |
final float yTargetVelocity = computeTargetVelocity( |
| 469 |
VERTICAL, event.getY(), v.getHeight(), mTarget.getHeight()); |
| 470 |
mScroller.setTargetVelocity(xTargetVelocity, yTargetVelocity); |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if (!mAnimating && shouldAnimate()) { |
| 475 |
startAnimating(); |
| 476 |
} |
| 477 |
break; |
| 478 |
case MotionEvent.ACTION_UP: |
| 479 |
case MotionEvent.ACTION_CANCEL: |
| 480 |
requestStop(); |
| 481 |
break; |
| 482 |
} |
| 483 |
|
| 484 |
return mExclusive && mAnimating; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
private boolean shouldAnimate() { |
| 491 |
final ClampedScroller scroller = mScroller; |
| 492 |
final int verticalDirection = scroller.getVerticalDirection(); |
| 493 |
final int horizontalDirection = scroller.getHorizontalDirection(); |
| 494 |
|
| 495 |
return verticalDirection != 0 && canTargetScrollVertically(verticalDirection) |
| 496 |
|| horizontalDirection != 0 && canTargetScrollHorizontally(horizontalDirection); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
private void startAnimating() { |
| 503 |
if (mRunnable == null) { |
| 504 |
mRunnable = new ScrollAnimationRunnable(); |
| 505 |
} |
| 506 |
|
| 507 |
mAnimating = true; |
| 508 |
mNeedsReset = true; |
| 509 |
|
| 510 |
if (!mAlreadyDelayed && mActivationDelay > 0) { |
| 511 |
mTarget.postOnAnimationDelayed(mRunnable, mActivationDelay); |
| 512 |
} else { |
| 513 |
mRunnable.run(); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
mAlreadyDelayed = true; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
private void requestStop() { |
| 527 |
if (mNeedsReset) { |
| 528 |
|
| 529 |
|
| 530 |
mAnimating = false; |
| 531 |
} else { |
| 532 |
mScroller.requestStop(); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
private float computeTargetVelocity( |
| 537 |
int direction, float coordinate, float srcSize, float dstSize) { |
| 538 |
final float relativeEdge = mRelativeEdges[direction]; |
| 539 |
final float maximumEdge = mMaximumEdges[direction]; |
| 540 |
final float value = getEdgeValue(relativeEdge, srcSize, maximumEdge, coordinate); |
| 541 |
if (value == 0) { |
| 542 |
|
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
final float relativeVelocity = mRelativeVelocity[direction]; |
| 547 |
final float minimumVelocity = mMinimumVelocity[direction]; |
| 548 |
final float maximumVelocity = mMaximumVelocity[direction]; |
| 549 |
final float targetVelocity = relativeVelocity * dstSize; |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
if (value > 0) { |
| 555 |
return constrain(value * targetVelocity, minimumVelocity, maximumVelocity); |
| 556 |
} else { |
| 557 |
return -constrain(-value * targetVelocity, minimumVelocity, maximumVelocity); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public abstract void scrollTargetBy(int deltaX, int deltaY); |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
public abstract boolean canTargetScrollHorizontally(int direction); |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public abstract boolean canTargetScrollVertically(int direction); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
private float getEdgeValue(float relativeValue, float size, float maxValue, float current) { |
| 605 |
|
| 606 |
final float edgeSize = constrain(relativeValue * size, NO_MIN, maxValue); |
| 607 |
final float valueLeading = constrainEdgeValue(current, edgeSize); |
| 608 |
final float valueTrailing = constrainEdgeValue(size - current, edgeSize); |
| 609 |
final float value = (valueTrailing - valueLeading); |
| 610 |
final float interpolated; |
| 611 |
if (value < 0) { |
| 612 |
interpolated = -mEdgeInterpolator.getInterpolation(-value); |
| 613 |
} else if (value > 0) { |
| 614 |
interpolated = mEdgeInterpolator.getInterpolation(value); |
| 615 |
} else { |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
return constrain(interpolated, -1, 1); |
| 620 |
} |
| 621 |
|
| 622 |
private float constrainEdgeValue(float current, float leading) { |
| 623 |
if (leading == 0) { |
| 624 |
return 0; |
| 625 |
} |
| 626 |
|
| 627 |
switch (mEdgeType) { |
| 628 |
case EDGE_TYPE_INSIDE: |
| 629 |
case EDGE_TYPE_INSIDE_EXTEND: |
| 630 |
if (current < leading) { |
| 631 |
if (current >= 0) { |
| 632 |
|
| 633 |
return 1f - current / leading; |
| 634 |
} else if (mAnimating && (mEdgeType == EDGE_TYPE_INSIDE_EXTEND)) { |
| 635 |
|
| 636 |
return 1f; |
| 637 |
} |
| 638 |
} |
| 639 |
break; |
| 640 |
case EDGE_TYPE_OUTSIDE: |
| 641 |
if (current < 0) { |
| 642 |
|
| 643 |
return current / -leading; |
| 644 |
} |
| 645 |
break; |
| 646 |
} |
| 647 |
|
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
private static int constrain(int value, int min, int max) { |
| 652 |
if (value > max) { |
| 653 |
return max; |
| 654 |
} else if (value < min) { |
| 655 |
return min; |
| 656 |
} else { |
| 657 |
return value; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
private static float constrain(float value, float min, float max) { |
| 662 |
if (value > max) { |
| 663 |
return max; |
| 664 |
} else if (value < min) { |
| 665 |
return min; |
| 666 |
} else { |
| 667 |
return value; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
private void cancelTargetTouch() { |
| 676 |
final long eventTime = SystemClock.uptimeMillis(); |
| 677 |
final MotionEvent cancel = MotionEvent.obtain( |
| 678 |
eventTime, eventTime, MotionEvent.ACTION_CANCEL, 0, 0, 0); |
| 679 |
mTarget.onTouchEvent(cancel); |
| 680 |
cancel.recycle(); |
| 681 |
} |
| 682 |
|
| 683 |
private class ScrollAnimationRunnable implements Runnable { |
| 684 |
@Override |
| 685 |
public void run() { |
| 686 |
if (!mAnimating) { |
| 687 |
return; |
| 688 |
} |
| 689 |
|
| 690 |
if (mNeedsReset) { |
| 691 |
mNeedsReset = false; |
| 692 |
mScroller.start(); |
| 693 |
} |
| 694 |
|
| 695 |
final ClampedScroller scroller = mScroller; |
| 696 |
if (scroller.isFinished() || !shouldAnimate()) { |
| 697 |
mAnimating = false; |
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
if (mNeedsCancel) { |
| 702 |
mNeedsCancel = false; |
| 703 |
cancelTargetTouch(); |
| 704 |
} |
| 705 |
|
| 706 |
scroller.computeScrollDelta(); |
| 707 |
|
| 708 |
final int deltaX = scroller.getDeltaX(); |
| 709 |
final int deltaY = scroller.getDeltaY(); |
| 710 |
scrollTargetBy(deltaX, deltaY); |
| 711 |
|
| 712 |
|
| 713 |
mTarget.postOnAnimation(this); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
private static class ClampedScroller { |
| 723 |
private int mRampUpDuration; |
| 724 |
private int mRampDownDuration; |
| 725 |
private float mTargetVelocityX; |
| 726 |
private float mTargetVelocityY; |
| 727 |
|
| 728 |
private long mStartTime; |
| 729 |
|
| 730 |
private long mDeltaTime; |
| 731 |
private int mDeltaX; |
| 732 |
private int mDeltaY; |
| 733 |
|
| 734 |
private long mStopTime; |
| 735 |
private float mStopValue; |
| 736 |
private int mEffectiveRampDown; |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
public ClampedScroller() { |
| 743 |
mStartTime = Long.MIN_VALUE; |
| 744 |
mStopTime = -1; |
| 745 |
mDeltaTime = 0; |
| 746 |
mDeltaX = 0; |
| 747 |
mDeltaY = 0; |
| 748 |
} |
| 749 |
|
| 750 |
public void setRampUpDuration(int durationMillis) { |
| 751 |
mRampUpDuration = durationMillis; |
| 752 |
} |
| 753 |
|
| 754 |
public void setRampDownDuration(int durationMillis) { |
| 755 |
mRampDownDuration = durationMillis; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
public void start() { |
| 762 |
mStartTime = AnimationUtils.currentAnimationTimeMillis(); |
| 763 |
mStopTime = -1; |
| 764 |
mDeltaTime = mStartTime; |
| 765 |
mStopValue = 0.5f; |
| 766 |
mDeltaX = 0; |
| 767 |
mDeltaY = 0; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
public void requestStop() { |
| 774 |
final long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 775 |
mEffectiveRampDown = constrain((int) (currentTime - mStartTime), 0, mRampDownDuration); |
| 776 |
mStopValue = getValueAt(currentTime); |
| 777 |
mStopTime = currentTime; |
| 778 |
} |
| 779 |
|
| 780 |
public boolean isFinished() { |
| 781 |
return mStopTime > 0 |
| 782 |
&& AnimationUtils.currentAnimationTimeMillis() > mStopTime + mEffectiveRampDown; |
| 783 |
} |
| 784 |
|
| 785 |
private float getValueAt(long currentTime) { |
| 786 |
if (currentTime < mStartTime) { |
| 787 |
return 0f; |
| 788 |
} else if (mStopTime < 0 || currentTime < mStopTime) { |
| 789 |
final long elapsedSinceStart = currentTime - mStartTime; |
| 790 |
return 0.5f * constrain(elapsedSinceStart / (float) mRampUpDuration, 0, 1); |
| 791 |
} else { |
| 792 |
final long elapsedSinceEnd = currentTime - mStopTime; |
| 793 |
return (1 - mStopValue) + mStopValue |
| 794 |
* constrain(elapsedSinceEnd / (float) mEffectiveRampDown, 0, 1); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
private float interpolateValue(float value) { |
| 806 |
return -4 * value * value + 4 * value; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
public void computeScrollDelta() { |
| 817 |
if (mDeltaTime == 0) { |
| 818 |
throw new RuntimeException("Cannot compute scroll delta before calling start()"); |
| 819 |
} |
| 820 |
|
| 821 |
final long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 822 |
final float value = getValueAt(currentTime); |
| 823 |
final float scale = interpolateValue(value); |
| 824 |
final long elapsedSinceDelta = currentTime - mDeltaTime; |
| 825 |
|
| 826 |
mDeltaTime = currentTime; |
| 827 |
mDeltaX = (int) (elapsedSinceDelta * scale * mTargetVelocityX); |
| 828 |
mDeltaY = (int) (elapsedSinceDelta * scale * mTargetVelocityY); |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
public void setTargetVelocity(float x, float y) { |
| 838 |
mTargetVelocityX = x; |
| 839 |
mTargetVelocityY = y; |
| 840 |
} |
| 841 |
|
| 842 |
public int getHorizontalDirection() { |
| 843 |
return (int) (mTargetVelocityX / Math.abs(mTargetVelocityX)); |
| 844 |
} |
| 845 |
|
| 846 |
public int getVerticalDirection() { |
| 847 |
return (int) (mTargetVelocityY / Math.abs(mTargetVelocityY)); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
public int getDeltaX() { |
| 855 |
return mDeltaX; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
public int getDeltaY() { |
| 863 |
return mDeltaY; |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
public static class AbsListViewAutoScroller extends AutoScrollHelper { |
| 872 |
private final AbsListView mTarget; |
| 873 |
|
| 874 |
public AbsListViewAutoScroller(AbsListView target) { |
| 875 |
super(target); |
| 876 |
|
| 877 |
mTarget = target; |
| 878 |
} |
| 879 |
|
| 880 |
@Override |
| 881 |
public void scrollTargetBy(int deltaX, int deltaY) { |
| 882 |
mTarget.scrollListBy(deltaY); |
| 883 |
} |
| 884 |
|
| 885 |
@Override |
| 886 |
public boolean canTargetScrollHorizontally(int direction) { |
| 887 |
|
| 888 |
return false; |
| 889 |
} |
| 890 |
|
| 891 |
@Override |
| 892 |
public boolean canTargetScrollVertically(int direction) { |
| 893 |
final AbsListView target = mTarget; |
| 894 |
final int itemCount = target.getCount(); |
| 895 |
if (itemCount == 0) { |
| 896 |
return false; |
| 897 |
} |
| 898 |
|
| 899 |
final int childCount = target.getChildCount(); |
| 900 |
final int firstPosition = target.getFirstVisiblePosition(); |
| 901 |
final int lastPosition = firstPosition + childCount; |
| 902 |
|
| 903 |
if (direction > 0) { |
| 904 |
|
| 905 |
if (lastPosition >= itemCount) { |
| 906 |
final View lastView = target.getChildAt(childCount - 1); |
| 907 |
if (lastView.getBottom() <= target.getHeight()) { |
| 908 |
return false; |
| 909 |
} |
| 910 |
} |
| 911 |
} else if (direction < 0) { |
| 912 |
|
| 913 |
if (firstPosition <= 0) { |
| 914 |
final View firstView = target.getChildAt(0); |
| 915 |
if (firstView.getTop() >= 0) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
} |
| 919 |
} else { |
| 920 |
|
| 921 |
|
| 922 |
return false; |
| 923 |
} |
| 924 |
|
| 925 |
return true; |
| 926 |
} |
| 927 |
} |
| 928 |
} |