diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/MagnifierView.java b/packages/SystemUI/src/com/android/systemui/screenshot/MagnifierView.java index 7a0ec4c520b2a..90f304262ea19 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/MagnifierView.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/MagnifierView.java @@ -16,6 +16,8 @@ package com.android.systemui.screenshot; +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; import android.annotation.NonNull; import android.content.Context; import android.content.res.TypedArray; @@ -28,6 +30,7 @@ import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; +import android.view.ViewPropertyAnimator; import androidx.annotation.Nullable; @@ -35,7 +38,7 @@ import com.android.systemui.R; /** * MagnifierView shows a full-res cropped circular display of a given ImageTileSet, contents and - * positioning dereived from events from a CropView to which it listens. + * positioning derived from events from a CropView to which it listens. * * Not meant to be a general-purpose magnifier! */ @@ -57,6 +60,20 @@ public class MagnifierView extends View implements CropView.CropInteractionListe private float mLastCropPosition; private CropView.CropBoundary mCropBoundary; + private ViewPropertyAnimator mTranslationAnimator; + private final Animator.AnimatorListener mTranslationAnimatorListener = + new AnimatorListenerAdapter() { + @Override + public void onAnimationCancel(Animator animation) { + mTranslationAnimator = null; + } + + @Override + public void onAnimationEnd(Animator animation) { + mTranslationAnimator = null; + } + }; + public MagnifierView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } @@ -133,6 +150,8 @@ public class MagnifierView extends View implements CropView.CropInteractionListe public void onCropMotionEvent(MotionEvent event, CropView.CropBoundary boundary, float cropPosition, int cropPositionPx) { mCropBoundary = boundary; + boolean touchOnRight = event.getX() > getParentWidth() / 2; + float translateXTarget = touchOnRight ? 0 : getParentWidth() - getWidth(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastCropPosition = cropPosition; @@ -144,11 +163,22 @@ public class MagnifierView extends View implements CropView.CropInteractionListe setAlpha(0f); setTranslationX((getParentWidth() - getWidth()) / 2); setVisibility(View.VISIBLE); - boolean touchOnRight = event.getX() > getParentWidth() / 2; - float translateXTarget = touchOnRight ? 0 : getParentWidth() - getWidth(); - animate().alpha(1f).translationX(translateXTarget).scaleX(1f).scaleY(1f).start(); + mTranslationAnimator = + animate().alpha(1f).translationX(translateXTarget).scaleX(1f).scaleY(1f); + mTranslationAnimator.setListener(mTranslationAnimatorListener); + mTranslationAnimator.start(); break; case MotionEvent.ACTION_MOVE: + // The touch is near the middle if it's within 10% of the center point. + // We don't want to animate horizontally if the touch is near the middle. + boolean nearMiddle = Math.abs(event.getX() - getParentWidth() / 2) + < getParentWidth() / 10f; + boolean viewOnLeft = getTranslationX() < (getParentWidth() - getWidth()) / 2; + if (!nearMiddle && viewOnLeft != touchOnRight && mTranslationAnimator == null) { + mTranslationAnimator = animate().translationX(translateXTarget); + mTranslationAnimator.setListener(mTranslationAnimatorListener); + mTranslationAnimator.start(); + } mLastCropPosition = cropPosition; setTranslationY(cropPositionPx - getHeight() / 2); invalidate();