[go: nahoru, domu]

1/*
2 * Copyright (C) 2014 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
17package android.support.v7.view;
18
19import android.support.v4.view.ViewPropertyAnimatorCompat;
20import android.support.v4.view.ViewPropertyAnimatorListener;
21import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
22import android.view.View;
23import android.view.animation.Interpolator;
24
25import java.util.ArrayList;
26
27/**
28 * A very naive implementation of a set of
29 * {@link android.support.v4.view.ViewPropertyAnimatorCompat}.
30 *
31 * @hide
32 */
33public class ViewPropertyAnimatorCompatSet {
34
35    private final ArrayList<ViewPropertyAnimatorCompat> mAnimators;
36
37    private long mDuration = -1;
38    private Interpolator mInterpolator;
39    private ViewPropertyAnimatorListener mListener;
40
41    private boolean mIsStarted;
42
43    public ViewPropertyAnimatorCompatSet() {
44        mAnimators = new ArrayList<ViewPropertyAnimatorCompat>();
45    }
46
47    public ViewPropertyAnimatorCompatSet play(ViewPropertyAnimatorCompat animator) {
48        if (!mIsStarted) {
49            mAnimators.add(animator);
50        }
51        return this;
52    }
53
54    public ViewPropertyAnimatorCompatSet playSequentially(ViewPropertyAnimatorCompat anim1,
55            ViewPropertyAnimatorCompat anim2) {
56        mAnimators.add(anim1);
57        anim2.setStartDelay(anim1.getDuration());
58        mAnimators.add(anim2);
59        return this;
60    }
61
62    public void start() {
63        if (mIsStarted) return;
64        for (ViewPropertyAnimatorCompat animator : mAnimators) {
65            if (mDuration >= 0) {
66                animator.setDuration(mDuration);
67            }
68            if (mInterpolator != null) {
69                animator.setInterpolator(mInterpolator);
70            }
71            if (mListener != null) {
72                animator.setListener(mProxyListener);
73            }
74            animator.start();
75        }
76
77        mIsStarted = true;
78    }
79
80    private void onAnimationsEnded() {
81        mIsStarted = false;
82    }
83
84    public void cancel() {
85        if (!mIsStarted) {
86            return;
87        }
88        for (ViewPropertyAnimatorCompat animator : mAnimators) {
89            animator.cancel();
90        }
91        mIsStarted = false;
92    }
93
94    public ViewPropertyAnimatorCompatSet setDuration(long duration) {
95        if (!mIsStarted) {
96            mDuration = duration;
97        }
98        return this;
99    }
100
101    public ViewPropertyAnimatorCompatSet setInterpolator(Interpolator interpolator) {
102        if (!mIsStarted) {
103            mInterpolator = interpolator;
104        }
105        return this;
106    }
107
108    public ViewPropertyAnimatorCompatSet setListener(ViewPropertyAnimatorListener listener) {
109        if (!mIsStarted) {
110            mListener = listener;
111        }
112        return this;
113    }
114
115    private final ViewPropertyAnimatorListenerAdapter mProxyListener
116            = new ViewPropertyAnimatorListenerAdapter() {
117        private boolean mProxyStarted = false;
118        private int mProxyEndCount = 0;
119
120        @Override
121        public void onAnimationStart(View view) {
122            if (mProxyStarted) {
123                return;
124            }
125            mProxyStarted = true;
126            if (mListener != null) {
127                mListener.onAnimationStart(null);
128            }
129        }
130
131        void onEnd() {
132            mProxyEndCount = 0;
133            mProxyStarted = false;
134            onAnimationsEnded();
135        }
136
137        @Override
138        public void onAnimationEnd(View view) {
139            if (++mProxyEndCount == mAnimators.size()) {
140                if (mListener != null) {
141                    mListener.onAnimationEnd(null);
142                }
143                onEnd();
144            }
145        }
146    };
147}
148