OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.ObjectAnimator; | 9 import android.animation.ObjectAnimator; |
10 import android.content.Context; | 10 import android.content.Context; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 public void init(View root, View controlContainer) { | 215 public void init(View root, View controlContainer) { |
216 mToolbarHeight = controlContainer.getHeight(); | 216 mToolbarHeight = controlContainer.getHeight(); |
217 mCurrentState = STATE_PEEK; | 217 mCurrentState = STATE_PEEK; |
218 | 218 |
219 // Listen to height changes on the root. | 219 // Listen to height changes on the root. |
220 root.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { | 220 root.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
221 public void onLayoutChange(View v, int left, int top, int right, int bottom, | 221 public void onLayoutChange(View v, int left, int top, int right, int bottom, |
222 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 222 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
223 mContainerHeight = bottom - top; | 223 mContainerHeight = bottom - top; |
224 updateSheetPeekHeight(mToolbarHeight, mContainerHeight); | 224 updateSheetPeekHeight(mToolbarHeight, mContainerHeight); |
225 setSheetState(mCurrentState, false); | 225 |
226 // If animating, don't reset the sheet position. | |
227 if (!isRunningSettleAnimation()) setSheetState(mCurrentState, fa lse); | |
Ian Wen
2017/01/17 19:23:59
I think you could cancel the current animation, an
mdjones
2017/01/17 21:59:31
This was for when the keyboard shows, onLayoutChan
| |
226 } | 228 } |
227 }); | 229 }); |
228 | 230 |
229 // Listen to height changes on the toolbar. | 231 // Listen to height changes on the toolbar. |
230 controlContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListen er() { | 232 controlContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListen er() { |
231 public void onLayoutChange(View v, int left, int top, int right, int bottom, | 233 public void onLayoutChange(View v, int left, int top, int right, int bottom, |
232 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 234 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
233 mToolbarHeight = bottom - top; | 235 mToolbarHeight = bottom - top; |
234 updateSheetPeekHeight(mToolbarHeight, mContainerHeight); | 236 updateSheetPeekHeight(mToolbarHeight, mContainerHeight); |
235 setSheetState(mCurrentState, false); | 237 |
238 // If animating, don't reset the sheet position. | |
239 if (!isRunningSettleAnimation()) setSheetState(mCurrentState, fa lse); | |
236 } | 240 } |
237 }); | 241 }); |
238 } | 242 } |
239 | 243 |
240 /** | 244 /** |
241 * Get the unadjusted version of a MotionEvent. | 245 * Get the unadjusted version of a MotionEvent. |
242 * @param e The original event. | 246 * @param e The original event. |
243 * @return The unadjusted version of the event. | 247 * @return The unadjusted version of the event. |
244 */ | 248 */ |
245 private MotionEvent getRawMotionEvent(MotionEvent e) { | 249 private MotionEvent getRawMotionEvent(MotionEvent e) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 private void setSheetOffsetFromBottom(float offset) { | 337 private void setSheetOffsetFromBottom(float offset) { |
334 setTranslationY(mContainerHeight - offset); | 338 setTranslationY(mContainerHeight - offset); |
335 } | 339 } |
336 | 340 |
337 /** | 341 /** |
338 * Move the sheet to the provided state. | 342 * Move the sheet to the provided state. |
339 * @param state The state to move the panel to. | 343 * @param state The state to move the panel to. |
340 * @param animate If true, the sheet will animate to the provided state, oth erwise it will | 344 * @param animate If true, the sheet will animate to the provided state, oth erwise it will |
341 * move there instantly. | 345 * move there instantly. |
342 */ | 346 */ |
343 private void setSheetState(int state, boolean animate) { | 347 public void setSheetState(int state, boolean animate) { |
344 mCurrentState = state; | 348 mCurrentState = state; |
345 | 349 |
346 if (animate) { | 350 if (animate) { |
347 createSettleAnimation(state); | 351 createSettleAnimation(state); |
348 } else { | 352 } else { |
349 setSheetOffsetFromBottom(getSheetHeightForState(state)); | 353 setSheetOffsetFromBottom(getSheetHeightForState(state)); |
350 } | 354 } |
351 } | 355 } |
352 | 356 |
353 /** | 357 /** |
358 * @return The current state of the bottom sheet. If the sheet is animating, this will be the | |
359 * state the sheet is animating to. | |
360 */ | |
361 public int getSheetState() { | |
362 return mCurrentState; | |
363 } | |
364 | |
365 /** | |
366 * If the animation to settle the sheet in one of its states is running. | |
367 * @return True if the animation is running. | |
368 */ | |
369 private boolean isRunningSettleAnimation() { | |
370 return mSettleAnimator != null; | |
371 } | |
372 | |
373 /** | |
354 * Get the height of the bottom sheet based on a provided state. | 374 * Get the height of the bottom sheet based on a provided state. |
355 * @param state The state to get the height from. | 375 * @param state The state to get the height from. |
356 * @return The height of the sheet at the provided state. | 376 * @return The height of the sheet at the provided state. |
357 */ | 377 */ |
358 private float getSheetHeightForState(int state) { | 378 private float getSheetHeightForState(int state) { |
359 return mStateRatios[state] * mContainerHeight; | 379 return mStateRatios[state] * mContainerHeight; |
360 } | 380 } |
361 | 381 |
362 /** | 382 /** |
363 * Get the target state of the sheet based on the sheet's height and velocit y. | 383 * Get the target state of the sheet based on the sheet's height and velocit y. |
(...skipping 27 matching lines...) Expand all Loading... | |
391 float distance = getSheetHeightForState(nextState) - lowerBound; | 411 float distance = getSheetHeightForState(nextState) - lowerBound; |
392 float thresholdToNextState = | 412 float thresholdToNextState = |
393 yVelocity < 0.0f ? THRESHOLD_TO_NEXT_STATE : 1.0f - THRESHOLD_TO _NEXT_STATE; | 413 yVelocity < 0.0f ? THRESHOLD_TO_NEXT_STATE : 1.0f - THRESHOLD_TO _NEXT_STATE; |
394 if ((sheetHeight - lowerBound) / distance > thresholdToNextState) { | 414 if ((sheetHeight - lowerBound) / distance > thresholdToNextState) { |
395 return nextState; | 415 return nextState; |
396 } else { | 416 } else { |
397 return prevState; | 417 return prevState; |
398 } | 418 } |
399 } | 419 } |
400 } | 420 } |
OLD | NEW |