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.FloatArrayEvaluator; 21 import android.animation.ObjectAnimator; 22 import android.util.Property; 23 24 import java.util.Arrays; 25 26 /** 27 * Animates a Folder preview item. 28 */ 29 class FolderPreviewItemAnim { 30 31 private static final Property<FolderPreviewItemAnim, float[]> PARAMS = 32 new Property<FolderPreviewItemAnim, float[]>(float[].class, "params") { 33 @Override 34 public float[] get(FolderPreviewItemAnim anim) { 35 sTempParamsArray[0] = anim.mParams.scale; 36 sTempParamsArray[1] = anim.mParams.transX; 37 sTempParamsArray[2] = anim.mParams.transY; 38 return sTempParamsArray; 39 } 40 41 @Override 42 public void set(FolderPreviewItemAnim anim, float[] value) { 43 anim.setParams(value); 44 } 45 }; 46 47 private static PreviewItemDrawingParams sTmpParams = new PreviewItemDrawingParams(0, 0, 0, 0); 48 private static final float[] sTempParamsArray = new float[3]; 49 50 private final ObjectAnimator mAnimator; 51 private final PreviewItemManager mItemManager; 52 private final PreviewItemDrawingParams mParams; 53 54 public final float[] finalState; 55 56 /** 57 * @param params layout params to animate 58 * @param index0 original index of the item to be animated 59 * @param items0 original number of items in the preview 60 * @param index1 new index of the item to be animated 61 * @param items1 new number of items in the preview 62 * @param duration duration in ms of the animation 63 * @param onCompleteRunnable runnable to execute upon animation completion 64 */ FolderPreviewItemAnim(PreviewItemManager itemManager, PreviewItemDrawingParams params, int index0, int items0, int index1, int items1, int duration, final Runnable onCompleteRunnable)65 FolderPreviewItemAnim(PreviewItemManager itemManager, 66 PreviewItemDrawingParams params, int index0, int items0, int index1, int items1, 67 int duration, final Runnable onCompleteRunnable) { 68 mItemManager = itemManager; 69 mParams = params; 70 71 mItemManager.computePreviewItemDrawingParams(index1, items1, sTmpParams); 72 finalState = new float[] {sTmpParams.scale, sTmpParams.transX, sTmpParams.transY}; 73 74 mItemManager.computePreviewItemDrawingParams(index0, items0, sTmpParams); 75 float[] startState = new float[] {sTmpParams.scale, sTmpParams.transX, sTmpParams.transY}; 76 77 mAnimator = ObjectAnimator.ofObject(this, PARAMS, new FloatArrayEvaluator(), 78 startState, finalState); 79 mAnimator.addListener(new AnimatorListenerAdapter() { 80 @Override 81 public void onAnimationEnd(Animator animation) { 82 if (onCompleteRunnable != null) { 83 onCompleteRunnable.run(); 84 } 85 params.anim = null; 86 } 87 }); 88 mAnimator.setDuration(duration); 89 } 90 setParams(float[] values)91 private void setParams(float[] values) { 92 mParams.scale = values[0]; 93 mParams.transX = values[1]; 94 mParams.transY = values[2]; 95 mItemManager.onParamsChanged(); 96 } 97 start()98 public void start() { 99 mAnimator.start(); 100 } 101 cancel()102 public void cancel() { 103 mAnimator.cancel(); 104 } 105 hasEqualFinalState(FolderPreviewItemAnim anim)106 public boolean hasEqualFinalState(FolderPreviewItemAnim anim) { 107 return Arrays.equals(finalState, anim.finalState); 108 109 } 110 } 111