InterruptibleInOutAnimator.java revision 150fbab7de7df45ce0e2d08fb0f0be87ff091c2f
1/*
2 * Copyright (C) 2010 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 com.android.launcher2;
18
19import android.animation.ValueAnimator;
20import android.util.Log;
21
22/**
23 * A convenience class for two-way animations, e.g. a fadeIn/fadeOut animation.
24 * With a regular ValueAnimator, if you call reverse to show the 'out' animation, you'll get
25 * a frame-by-frame mirror of the 'in' animation -- i.e., the interpolated values will
26 * be exactly reversed. Using this class, both the 'in' and the 'out' animation use the
27 * interpolator in the same direction.
28 */
29public class InterruptibleInOutAnimator extends ValueAnimator {
30    private long mOriginalDuration;
31    private Object mOriginalFromValue;
32    private Object mOriginalToValue;
33
34    public InterruptibleInOutAnimator(long duration, Object fromValue, Object toValue) {
35        super(duration, fromValue, toValue);
36        mOriginalDuration = duration;
37        mOriginalFromValue = fromValue;
38        mOriginalToValue = toValue;
39    }
40
41    private void animate(Object fromValue, Object toValue) {
42        // This only makes sense when it's running in the opposite direction, or stopped.
43        setDuration(mOriginalDuration - getCurrentPlayTime());
44
45        final Object startValue = isRunning() ? getAnimatedValue() : fromValue;
46        cancel();
47        setValues(startValue, toValue);
48        start();
49    }
50
51    /**
52     * This is the equivalent of calling Animator.start(), except that it can be called when
53     * the animation is running in the opposite direction, in which case we reverse
54     * direction and animate for a correspondingly shorter duration.
55     */
56    public void animateIn() {
57        animate(mOriginalFromValue, mOriginalToValue);
58    }
59
60    /**
61     * This is the roughly the equivalent of calling Animator.reverse(), except that it uses the
62     * same interpolation curve as animateIn(), rather than mirroring it. Also, like animateIn(),
63     * if the animation is currently running in the opposite direction, we reverse
64     * direction and animate for a correspondingly shorter duration.
65     */
66    public void animateOut() {
67        animate(mOriginalToValue, mOriginalFromValue);
68    }
69}
70