1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.folder; 17 18 import android.animation.Animator; 19 import android.animation.AnimatorListenerAdapter; 20 import android.animation.ValueAnimator; 21 22 import com.android.launcher3.LauncherAnimUtils; 23 24 /** 25 * Animates a Folder preview item. 26 */ 27 class FolderPreviewItemAnim { 28 29 private static PreviewItemDrawingParams sTmpParams = new PreviewItemDrawingParams(0, 0, 0, 0); 30 31 private ValueAnimator mValueAnimator; 32 33 float finalScale; 34 float finalTransX; 35 float finalTransY; 36 37 /** 38 * @param params layout params to animate 39 * @param index0 original index of the item to be animated 40 * @param items0 original number of items in the preview 41 * @param index1 new index of the item to be animated 42 * @param items1 new number of items in the preview 43 * @param duration duration in ms of the animation 44 * @param onCompleteRunnable runnable to execute upon animation completion 45 */ 46 FolderPreviewItemAnim(final PreviewItemManager previewItemManager, 47 final PreviewItemDrawingParams params, int index0, int items0, int index1, int items1, 48 int duration, final Runnable onCompleteRunnable) { 49 previewItemManager.computePreviewItemDrawingParams(index1, items1, sTmpParams); 50 51 finalScale = sTmpParams.scale; 52 finalTransX = sTmpParams.transX; 53 finalTransY = sTmpParams.transY; 54 55 previewItemManager.computePreviewItemDrawingParams(index0, items0, sTmpParams); 56 57 final float scale0 = sTmpParams.scale; 58 final float transX0 = sTmpParams.transX; 59 final float transY0 = sTmpParams.transY; 60 61 mValueAnimator = LauncherAnimUtils.ofFloat(0f, 1.0f); 62 mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){ 63 public void onAnimationUpdate(ValueAnimator animation) { 64 float progress = animation.getAnimatedFraction(); 65 66 params.transX = transX0 + progress * (finalTransX - transX0); 67 params.transY = transY0 + progress * (finalTransY - transY0); 68 params.scale = scale0 + progress * (finalScale - scale0); 69 previewItemManager.onParamsChanged(); 70 } 71 }); 72 mValueAnimator.addListener(new AnimatorListenerAdapter() { 73 @Override 74 public void onAnimationEnd(Animator animation) { 75 if (onCompleteRunnable != null) { 76 onCompleteRunnable.run(); 77 } 78 params.anim = null; 79 } 80 }); 81 mValueAnimator.setDuration(duration); 82 } 83 84 public void start() { 85 mValueAnimator.start(); 86 } 87 88 public void cancel() { 89 mValueAnimator.cancel(); 90 } 91 92 public boolean hasEqualFinalState(FolderPreviewItemAnim anim) { 93 return finalTransY == anim.finalTransY && finalTransX == anim.finalTransX && 94 finalScale == anim.finalScale; 95 96 } 97 } 98