PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/android/java/src/org/chromium/chrome/browser/widget/ProgressAnimationSmooth.java

https://gitlab.com/jonnialva90/iridium-browser
Java | 97 lines | 51 code | 12 blank | 34 comment | 7 complexity | 4b32da08078ea630905f9f9bb718ea0a MD5 | raw file
  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.widget;
  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 = 2.0f * DECELERATION
  18. / ((DECELERATION + ACCELERATION) * ACCELERATION);
  19. private static final float CONSTANT_3 = DECELERATION
  20. / ((DECELERATION + ACCELERATION) * ACCELERATION * ACCELERATION);
  21. private float mProgress;
  22. private float mVelocity;
  23. @Override
  24. public void reset() {
  25. mProgress = 0.0f;
  26. mVelocity = 0.0f;
  27. }
  28. @Override
  29. public float updateProgress(float targetProgress, float frameTimeSec, int resolution) {
  30. assert mProgress <= targetProgress;
  31. final float acceleratingDuration = computeAcceleratingDuration(
  32. targetProgress, frameTimeSec);
  33. final float deceleratingDuration = frameTimeSec - acceleratingDuration;
  34. if (acceleratingDuration > 0.0f) {
  35. float velocityChange = (targetProgress == 1.0f ? FINISHING_ACCELERATION : ACCELERATION)
  36. * acceleratingDuration;
  37. mProgress += (mVelocity + 0.5f * velocityChange) * acceleratingDuration;
  38. mVelocity += velocityChange;
  39. }
  40. if (deceleratingDuration > 0.0f) {
  41. float velocityChange = -DECELERATION * deceleratingDuration;
  42. mProgress += (mVelocity + 0.5f * velocityChange) * deceleratingDuration;
  43. mVelocity += velocityChange;
  44. }
  45. mProgress = Math.min(mProgress, targetProgress);
  46. if (targetProgress - mProgress < 0.5f / resolution) {
  47. mProgress = targetProgress;
  48. mVelocity = 0.0f;
  49. }
  50. return mProgress;
  51. }
  52. /**
  53. * Computes and returns accelerating duration.
  54. *
  55. * Symbol Description Corresponding variable
  56. * v_0 Initial velocity mVelocity
  57. * A Acceleration ACCELERATION
  58. * D Deceleration DECELERATION
  59. * d_A Accelerating duration
  60. * d_D Decelerating duration
  61. *
  62. * Given the initial position and the initial velocity, we assume that it accelerates constantly
  63. * and then decelerates constantly.
  64. *
  65. * We want to stop smoothly when it reaches the end. Thus zero velocity at the end:
  66. * v_0 + A d_A - D d_D = 0
  67. * Equation image: 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: 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
  73. *
  74. * This function solves d_A from the above equations.
  75. */
  76. private float computeAcceleratingDuration(float targetProgress, float frameTimeSec) {
  77. if (targetProgress == 1.0f) {
  78. return frameTimeSec;
  79. } else {
  80. float maxAcceleratingDuration = CONSTANT_1 * mVelocity + (float) Math.sqrt(
  81. CONSTANT_2 * (targetProgress - mProgress) + CONSTANT_3 * mVelocity * mVelocity);
  82. return Math.max(0, Math.min(frameTimeSec, maxAcceleratingDuration));
  83. }
  84. }
  85. }