private void animateClose(ObjectAnimator backgroundAnimator) { AnimationRect rect = getArguments().getParcelable("rect"); if (rect == null) { photoView.animate().alpha(0); backgroundAnimator.start(); return; } final Rect startBounds = rect.scaledBitmapRect; final Rect finalBounds = AnimationUtility.getBitmapRectFromImageView(photoView); if (finalBounds == null) { photoView.animate().alpha(0); backgroundAnimator.start(); return; } if (Utility.isDevicePort() != rect.isScreenPortrait) { photoView.animate().alpha(0); backgroundAnimator.start(); return; } float startScale; if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width() / startBounds.height()) { startScale = (float) startBounds.height() / finalBounds.height(); } else { startScale = (float) startBounds.width() / finalBounds.width(); } final float startScaleFinal = startScale; int deltaTop = startBounds.top - finalBounds.top; int deltaLeft = startBounds.left - finalBounds.left; photoView.setPivotY((photoView.getHeight() - finalBounds.height()) / 2); photoView.setPivotX((photoView.getWidth() - finalBounds.width()) / 2); photoView .animate() .translationX(deltaLeft) .translationY(deltaTop) .scaleY(startScaleFinal) .scaleX(startScaleFinal) .setDuration(ANIMATION_DURATION) .setInterpolator(new AccelerateDecelerateInterpolator()) .withEndAction( new Runnable() { @Override public void run() { photoView .animate() .alpha(0.0f) .setDuration(200) .withEndAction( new Runnable() { @Override public void run() {} }); } }); AnimatorSet animationSet = new AnimatorSet(); animationSet.setDuration(ANIMATION_DURATION); animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); animationSet.playTogether(backgroundAnimator); animationSet.playTogether( ObjectAnimator.ofFloat( photoView, "clipBottom", 0, AnimationRect.getClipBottom(rect, finalBounds))); animationSet.playTogether( ObjectAnimator.ofFloat( photoView, "clipRight", 0, AnimationRect.getClipRight(rect, finalBounds))); animationSet.playTogether( ObjectAnimator.ofFloat( photoView, "clipTop", 0, AnimationRect.getClipTop(rect, finalBounds))); animationSet.playTogether( ObjectAnimator.ofFloat( photoView, "clipLeft", 0, AnimationRect.getClipLeft(rect, finalBounds))); animationSet.start(); }