OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
6 | 6 |
7 import android.animation.Animator; | 7 import android.animation.Animator; |
8 import android.animation.AnimatorListenerAdapter; | 8 import android.animation.AnimatorListenerAdapter; |
9 import android.animation.AnimatorSet; | 9 import android.animation.AnimatorSet; |
10 import android.animation.ValueAnimator; | 10 import android.animation.ValueAnimator; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 /** If the layout is RTL. */ | 54 /** If the layout is RTL. */ |
55 private boolean mIsRtl; | 55 private boolean mIsRtl; |
56 | 56 |
57 /** The update listener for the animation. */ | 57 /** The update listener for the animation. */ |
58 private ProgressBarUpdateListener mListener; | 58 private ProgressBarUpdateListener mListener; |
59 | 59 |
60 /** The last fraction of the animation that was drawn. */ | 60 /** The last fraction of the animation that was drawn. */ |
61 private float mLastAnimatedFraction; | 61 private float mLastAnimatedFraction; |
62 | 62 |
63 /** | 63 /** |
| 64 * If the animation is currently running. This is needed because Animator.is
Started() is not |
| 65 * reliable on JellyBean. |
| 66 */ |
| 67 private boolean mIsAnimationRunning; |
| 68 |
| 69 /** |
64 * An animation update listener that moves an ImageView across the progress
bar. | 70 * An animation update listener that moves an ImageView across the progress
bar. |
65 */ | 71 */ |
66 private class ProgressBarUpdateListener implements AnimatorUpdateListener { | 72 private class ProgressBarUpdateListener implements AnimatorUpdateListener { |
67 @Override | 73 @Override |
68 public void onAnimationUpdate(ValueAnimator animation) { | 74 public void onAnimationUpdate(ValueAnimator animation) { |
69 mLastAnimatedFraction = animation.getAnimatedFraction(); | 75 mLastAnimatedFraction = animation.getAnimatedFraction(); |
70 updateAnimation(mLastAnimatedFraction); | 76 updateAnimation(mLastAnimatedFraction); |
71 } | 77 } |
72 } | 78 } |
73 | 79 |
74 /** | 80 /** |
75 * @param context The Context for this view. | 81 * @param context The Context for this view. |
76 * @param height The LayoutParams for this view. | 82 * @param height The LayoutParams for this view. |
77 */ | 83 */ |
78 public ToolbarProgressBarAnimatingView(Context context, LayoutParams layoutP
arams) { | 84 public ToolbarProgressBarAnimatingView(Context context, LayoutParams layoutP
arams) { |
79 super(context); | 85 super(context); |
80 | |
81 setLayoutParams(layoutParams); | 86 setLayoutParams(layoutParams); |
82 mIsRtl = LocalizationUtils.isLayoutRtl(); | 87 mIsRtl = LocalizationUtils.isLayoutRtl(); |
83 | 88 |
84 mAnimationDrawable = new ColorDrawable(Color.WHITE); | 89 mAnimationDrawable = new ColorDrawable(Color.WHITE); |
85 | 90 |
86 setImageDrawable(mAnimationDrawable); | 91 setImageDrawable(mAnimationDrawable); |
87 | 92 |
88 mListener = new ProgressBarUpdateListener(); | 93 mListener = new ProgressBarUpdateListener(); |
89 mAnimatorSet = new AnimatorSet(); | 94 mAnimatorSet = new AnimatorSet(); |
90 | 95 |
(...skipping 19 matching lines...) Expand all Loading... |
110 mAnimatorSet.start(); | 115 mAnimatorSet.start(); |
111 } | 116 } |
112 }); | 117 }); |
113 } | 118 } |
114 | 119 |
115 /** | 120 /** |
116 * Start the animation if it hasn't been already. | 121 * Start the animation if it hasn't been already. |
117 */ | 122 */ |
118 public void startAnimation() { | 123 public void startAnimation() { |
119 mIsCanceled = false; | 124 mIsCanceled = false; |
120 if (!mAnimatorSet.isStarted()) { | 125 if (!mIsAnimationRunning) { |
| 126 mIsAnimationRunning = true; |
121 // Set the initial start delay to 0ms so it starts immediately. | 127 // Set the initial start delay to 0ms so it starts immediately. |
122 mAnimatorSet.setStartDelay(0); | 128 mAnimatorSet.setStartDelay(0); |
123 | 129 |
124 // Reset position. | 130 // Reset position. |
125 setScaleX(0.0f); | 131 setScaleX(0.0f); |
126 setTranslationX(0.0f); | 132 setTranslationX(0.0f); |
127 mAnimatorSet.start(); | 133 mAnimatorSet.start(); |
128 | 134 |
129 // Fade in to look nice on sites that trigger many loads that end qu
ickly. | 135 // Fade in to look nice on sites that trigger many loads that end qu
ickly. |
130 animate().alpha(1.0f) | 136 animate().alpha(1.0f) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 */ | 179 */ |
174 public boolean isRunning() { | 180 public boolean isRunning() { |
175 return mAnimatorSet.isStarted(); | 181 return mAnimatorSet.isStarted(); |
176 } | 182 } |
177 | 183 |
178 /** | 184 /** |
179 * Cancel the animation. | 185 * Cancel the animation. |
180 */ | 186 */ |
181 public void cancelAnimation() { | 187 public void cancelAnimation() { |
182 mIsCanceled = true; | 188 mIsCanceled = true; |
183 mAnimatorSet.cancel(); | 189 mIsAnimationRunning = false; |
184 // Reset position and alpha. | 190 // Reset position and alpha. |
185 setScaleX(0.0f); | 191 setScaleX(0.0f); |
186 setTranslationX(0.0f); | 192 setTranslationX(0.0f); |
187 animate().cancel(); | 193 animate().cancel(); |
188 setAlpha(0.0f); | 194 setAlpha(0.0f); |
189 mLastAnimatedFraction = 0.0f; | 195 mLastAnimatedFraction = 0.0f; |
190 } | 196 } |
191 | 197 |
192 /** | 198 /** |
193 * Update info about the progress bar holding this animating block. | 199 * Update info about the progress bar holding this animating block. |
194 * @param progressWidth The width of the contaiing progress bar. | 200 * @param progressWidth The width of the contaiing progress bar. |
195 */ | 201 */ |
196 public void update(float progressWidth) { | 202 public void update(float progressWidth) { |
197 mProgressWidth = progressWidth; | 203 mProgressWidth = progressWidth; |
198 updateAnimation(mLastAnimatedFraction); | 204 updateAnimation(mLastAnimatedFraction); |
199 } | 205 } |
200 | 206 |
201 /** | 207 /** |
202 * @param color The Android color that the animating bar should be. | 208 * @param color The Android color that the animating bar should be. |
203 */ | 209 */ |
204 public void setColor(int color) { | 210 public void setColor(int color) { |
205 mAnimationDrawable.setColor(color); | 211 mAnimationDrawable.setColor(color); |
206 } | 212 } |
207 } | 213 } |
OLD | NEW |