PageRenderTime 64ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/browser/ui/android/toolbar/java/src/org/chromium/chrome/browser/toolbar/ProgressAnimationSmooth.java

https://github.com/chromium/chromium
Java | 98 lines | 51 code | 11 blank | 36 comment | 7 complexity | f4c8fe6aba5239fc8aef8600b745bfd9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. package org.chromium.chrome.browser.toolbar;
  5. /**
  6. * Progress bar animation logic that smoothly accelerates in the beginning and smoothly decelerates
  7. * towards the end. The model is applying a constant acceleration followed by a constant
  8. * deceleration.
  9. */
  10. class ProgressAnimationSmooth implements ToolbarProgressBar.AnimationLogic {
  11. // The (de)acceleration unit is progress per second squared where 0 <= progress <= 1.
  12. private static final float FINISHING_ACCELERATION = 7.0f;
  13. private static final float ACCELERATION = 0.15f;
  14. private static final float DECELERATION = 0.15f;
  15. // Precomputed constants
  16. private static final float CONSTANT_1 = -1.0f / ACCELERATION;
  17. private static final float CONSTANT_2 =
  18. 2.0f * DECELERATION / ((DECELERATION + ACCELERATION) * ACCELERATION);
  19. private static final float CONSTANT_3 =
  20. DECELERATION / ((DECELERATION + ACCELERATION) * ACCELERATION * ACCELERATION);
  21. private float mProgress;
  22. private float mVelocity;
  23. @Override
  24. public void reset(float startProgress) {
  25. mProgress = startProgress;
  26. mVelocity = 0.0f;
  27. }
  28. @Override
  29. public float updateProgress(float targetProgress, float frameTimeSec, int resolution) {
  30. final float acceleratingDuration =
  31. computeAcceleratingDuration(targetProgress, frameTimeSec);
  32. final float deceleratingDuration = frameTimeSec - acceleratingDuration;
  33. if (acceleratingDuration > 0.0f) {
  34. float velocityChange = (targetProgress == 1.0f ? FINISHING_ACCELERATION : ACCELERATION)
  35. * acceleratingDuration;
  36. mProgress += (mVelocity + 0.5f * velocityChange) * acceleratingDuration;
  37. mVelocity += velocityChange;
  38. }
  39. if (deceleratingDuration > 0.0f) {
  40. float velocityChange = -DECELERATION * deceleratingDuration;
  41. mProgress += (mVelocity + 0.5f * velocityChange) * deceleratingDuration;
  42. mVelocity += velocityChange;
  43. }
  44. mProgress = Math.min(mProgress, targetProgress);
  45. if (targetProgress - mProgress < 0.5f / resolution) {
  46. mProgress = targetProgress;
  47. mVelocity = 0.0f;
  48. }
  49. return mProgress;
  50. }
  51. /**
  52. * Computes and returns accelerating duration.
  53. *
  54. * Symbol Description Corresponding variable
  55. * v_0 Initial velocity mVelocity
  56. * A Acceleration ACCELERATION
  57. * D Deceleration DECELERATION
  58. * d_A Accelerating duration
  59. * d_D Decelerating duration
  60. *
  61. * Given the initial position and the initial velocity, we assume that it accelerates constantly
  62. * and then decelerates constantly.
  63. *
  64. * We want to stop smoothly when it reaches the end. Thus zero velocity at the end:
  65. * v_0 + A d_A - D d_D = 0
  66. * Equation image:
  67. * http://www.HostMath.com/Show.aspx?Code=v_0%20%2B%20A%20d_A%20-%20D%20d_D%20%3D%200
  68. *
  69. * The traveled distance should be (targetProgress - mProgress):
  70. * targetProgress - mProgress =
  71. * \int_0^{d_A} (v_0 + A t) dt + \int_0^{d_D} (v_{0} + A d_A - D t) dt
  72. * Equation image:
  73. * http://www.HostMath.com/Show.aspx?Code=targetProgress%20-%20mProgress%20%3D%20%5Cint_0%5E%7Bd_A%7D%20(v_0%20%2B%20A%20t)%20dt%20%2B%20%5Cint_0%5E%7Bd_D%7D%20(v_%7B0%7D%20%2B%20A%20d_A%20-%20D%20t)dt
  74. *
  75. * This function solves d_A from the above equations.
  76. */
  77. private float computeAcceleratingDuration(float targetProgress, float frameTimeSec) {
  78. if (targetProgress == 1.0f) {
  79. return frameTimeSec;
  80. } else {
  81. float maxAcceleratingDuration = CONSTANT_1 * mVelocity
  82. + (float) Math.sqrt(CONSTANT_2 * (targetProgress - mProgress)
  83. + CONSTANT_3 * mVelocity * mVelocity);
  84. return Math.max(0, Math.min(frameTimeSec, maxAcceleratingDuration));
  85. }
  86. }
  87. }