 Chromium Code Reviews
 Chromium Code Reviews Issue 2868403003:
  added scale animation for context menu  (Closed)
    
  
    Issue 2868403003:
  added scale animation for context menu  (Closed) 
  | Index: chrome/android/java/src/org/chromium/chrome/browser/widget/ContextMenuDialog.java | 
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/widget/ContextMenuDialog.java b/chrome/android/java/src/org/chromium/chrome/browser/widget/ContextMenuDialog.java | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..793f541e5e4e866d971002d2dbb6520aeca7b878 | 
| --- /dev/null | 
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/widget/ContextMenuDialog.java | 
| @@ -0,0 +1,100 @@ | 
| +// Copyright 2017 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +package org.chromium.chrome.browser.widget; | 
| + | 
| +import android.app.Activity; | 
| +import android.support.v4.view.animation.LinearOutSlowInInterpolator; | 
| +import android.view.MotionEvent; | 
| +import android.view.View; | 
| +import android.view.animation.Animation; | 
| +import android.view.animation.Animation.AnimationListener; | 
| +import android.view.animation.ScaleAnimation; | 
| + | 
| +/** | 
| + * AlwaysDismissedDialog that ensures that the dialog animates when it's being dismissed. | 
| 
Theresa
2017/05/24 20:16:33
nit: The class comment and class name don't really
 
Daniel Park
2017/05/24 22:34:31
Done.
 | 
| + */ | 
| +public class ContextMenuDialog extends AlwaysDismissedDialog { | 
| + private View mPagerView; | 
| + private float mContextMenuDestinationX; | 
| + private float mContextMenuDestinationY; | 
| + private int mContextMenuFirstLocationY; | 
| + | 
| + private static final int ANIMATION_IN_DURATION = 250; | 
| + // Animation out duration should be set to 60% of the animation in duration. | 
| + private static final int ANIMATION_OUT_DURATION = 150; | 
| + | 
| + public ContextMenuDialog(Activity ownerActivity, int theme) { | 
| + super(ownerActivity, theme); | 
| + } | 
| + | 
| + public void setExitAnimationParameters(View pagerView, float contextMenuDestinationX, | 
| 
Theresa
2017/05/24 20:16:33
nit: JavaDocs for this. Also, does it matter that
 
Daniel Park
2017/05/24 22:34:31
Done.
 | 
| + float contextMenuDestinationY, int contextMenuFirstLocationY) { | 
| + mPagerView = pagerView; | 
| + mContextMenuDestinationX = contextMenuDestinationX; | 
| + mContextMenuDestinationY = contextMenuDestinationY; | 
| + mContextMenuFirstLocationY = contextMenuFirstLocationY; | 
| + } | 
| + | 
| + @Override | 
| + public void dismiss() { | 
| + int[] contextMenuFinalLocation = new int[2]; | 
| + mPagerView.getLocationOnScreen(contextMenuFinalLocation); | 
| + // Recalculate mContextMenuDestinationY because the final tab shown may have caused the view | 
| + // height to change | 
| + mContextMenuDestinationY = mContextMenuDestinationY | 
| + + (mContextMenuFirstLocationY - contextMenuFinalLocation[1]); | 
| + | 
| + Animation exitAnimation = | 
| + getScaleAnimation(false, mContextMenuDestinationX, mContextMenuDestinationY); | 
| + exitAnimation.setAnimationListener(new AnimationListener() { | 
| + | 
| + @Override | 
| + public void onAnimationStart(Animation animation) {} | 
| + | 
| + @Override | 
| + public void onAnimationRepeat(Animation animation) {} | 
| + | 
| + @Override | 
| + public void onAnimationEnd(Animation animation) { | 
| + ContextMenuDialog.super.dismiss(); | 
| + } | 
| + }); | 
| + mPagerView.startAnimation(exitAnimation); | 
| + } | 
| + | 
| + @Override | 
| + public boolean onTouchEvent(MotionEvent event) { | 
| + if (event.getAction() == MotionEvent.ACTION_DOWN) { | 
| + this.dismiss(); | 
| + } | 
| + return true; | 
| + } | 
| + | 
| + /** | 
| + * @param isEnterAnimation Whether or not the animation will be for when the context menu enters | 
| + * or not. | 
| + * @param pivotX The X coordinate of the point about which the object is being scaled, specified | 
| + * as an absolute number where 0 is the left edge. | 
| + * @param pivotY The Y coordinate of the point about which the object is being scaled, specified | 
| + * as an absolute number where 0 is the top edge. | 
| + * @return Returns the scale animation for the context menu. | 
| + */ | 
| + | 
| + public Animation getScaleAnimation(boolean isEnterAnimation, float pivotX, float pivotY) { | 
| + float fromX = isEnterAnimation ? 0f : 1f; | 
| + float toX = isEnterAnimation ? 1f : 0f; | 
| + float fromY = fromX; | 
| + float toY = toX; | 
| + | 
| + ScaleAnimation animation = new ScaleAnimation( | 
| + fromX, toX, fromY, toY, Animation.ABSOLUTE, pivotX, Animation.ABSOLUTE, pivotY); | 
| + | 
| + long duration = isEnterAnimation ? ANIMATION_IN_DURATION : ANIMATION_OUT_DURATION; | 
| + | 
| + animation.setDuration(duration); | 
| + animation.setInterpolator(new LinearOutSlowInInterpolator()); | 
| + return animation; | 
| + } | 
| +} |