• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.anim;
17 
18 import static com.android.launcher3.anim.AnimatorPlaybackController.addAnimationHoldersRecur;
19 
20 import android.animation.Animator;
21 import android.animation.AnimatorSet;
22 import android.animation.ObjectAnimator;
23 import android.animation.TimeInterpolator;
24 import android.animation.ValueAnimator;
25 import android.util.FloatProperty;
26 
27 import com.android.launcher3.anim.AnimatorPlaybackController.Holder;
28 
29 import java.util.ArrayList;
30 
31 /**
32  * Utility class to keep track of a running animation.
33  *
34  * This class allows attaching end callbacks to an animation is intended to be used with
35  * {@link com.android.launcher3.anim.AnimatorPlaybackController}, since in that case
36  * AnimationListeners are not properly dispatched.
37  *
38  * TODO: Find a better name
39  */
40 public class PendingAnimation extends AnimatedPropertySetter {
41 
42     private final ArrayList<Holder> mAnimHolders = new ArrayList<>();
43     private final long mDuration;
44 
PendingAnimation(long duration)45     public PendingAnimation(long  duration) {
46         mDuration = duration;
47     }
48 
getDuration()49     public long getDuration() {
50         return mDuration;
51     }
52 
53     /**
54      * Utility method to sent an interpolator on an animation and add it to the list
55      */
add(Animator anim, TimeInterpolator interpolator, SpringProperty springProperty)56     public void add(Animator anim, TimeInterpolator interpolator, SpringProperty springProperty) {
57         anim.setInterpolator(interpolator);
58         add(anim, springProperty);
59     }
60 
61     @Override
add(Animator anim)62     public void add(Animator anim) {
63         add(anim, SpringProperty.DEFAULT);
64     }
65 
add(Animator a, SpringProperty springProperty)66     public void add(Animator a, SpringProperty springProperty) {
67         mAnim.play(a.setDuration(mDuration));
68         addAnimationHoldersRecur(a, mDuration, springProperty, mAnimHolders);
69     }
70 
71     /**
72      * Configures interpolator of the underlying AnimatorSet.
73      */
setInterpolator(TimeInterpolator interpolator)74     public void setInterpolator(TimeInterpolator interpolator) {
75         mAnim.setInterpolator(interpolator);
76     }
77 
addFloat(T target, FloatProperty<T> property, float from, float to, TimeInterpolator interpolator)78     public <T> void addFloat(T target, FloatProperty<T> property, float from, float to,
79             TimeInterpolator interpolator) {
80         Animator anim = ObjectAnimator.ofFloat(target, property, from, to);
81         anim.setInterpolator(interpolator);
82         add(anim);
83     }
84 
85     /**
86      * Creates and returns the underlying AnimatorSet
87      */
88     @Override
buildAnim()89     public AnimatorSet buildAnim() {
90         if (mAnimHolders.isEmpty()) {
91             // Add a placeholder animation to that the duration is respected
92             add(ValueAnimator.ofFloat(0, 1).setDuration(mDuration));
93         }
94         return super.buildAnim();
95     }
96 
97     /**
98      * Creates a controller for this animation
99      */
createPlaybackController()100     public AnimatorPlaybackController createPlaybackController() {
101         return new AnimatorPlaybackController(buildAnim(), mDuration, mAnimHolders);
102     }
103 }
104