1/*
2 * Copyright (C) 2016 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.server.wm.animation;
18
19import android.graphics.Rect;
20import android.view.animation.ClipRectAnimation;
21import android.view.animation.Interpolator;
22import android.view.animation.Transformation;
23import android.view.animation.TranslateAnimation;
24
25/**
26 * Special case of ClipRectAnimation that animates only the top/bottom
27 * dimensions of the clip, picking up the other dimensions from whatever is
28 * set on the transform already. In addition to that, information about a vertical translation
29 * animation can be specified so this animation simulates as the clip would be applied after instead
30 * of before applying the translation.
31 */
32public class ClipRectTBAnimation extends ClipRectAnimation {
33
34    private final int mFromTranslateY;
35    private final int mToTranslateY;
36    private final Interpolator mTranslateInterpolator;
37    private float mNormalizedTime;
38
39    /**
40     * Constructor. Passes in 0 for Left/Right parameters of ClipRectAnimation
41     */
42    public ClipRectTBAnimation(int fromT, int fromB, int toT, int toB,
43            int fromTranslateY, int toTranslateY, Interpolator translateInterpolator) {
44        super(0, fromT, 0, fromB, 0, toT, 0, toB);
45        mFromTranslateY = fromTranslateY;
46        mToTranslateY = toTranslateY;
47        mTranslateInterpolator = translateInterpolator;
48    }
49
50    @Override
51    public boolean getTransformation(long currentTime, Transformation outTransformation) {
52
53        // Hack: Because translation animation has a different interpolator, we need to duplicate
54        // code from Animation here and use it to calculate/store the uninterpolated normalized
55        // time.
56        final long startOffset = getStartOffset();
57        final long duration = getDuration();
58        float normalizedTime;
59        if (duration != 0) {
60            normalizedTime = ((float) (currentTime - (getStartTime() + startOffset))) /
61                    (float) duration;
62        } else {
63            // time is a step-change with a zero duration
64            normalizedTime = currentTime < getStartTime() ? 0.0f : 1.0f;
65        }
66        mNormalizedTime = normalizedTime;
67        return super.getTransformation(currentTime, outTransformation);
68    }
69
70    /**
71     * Calculates and sets clip rect on given transformation. It uses existing values
72     * on the Transformation for Left/Right clip parameters.
73     */
74    @Override
75    protected void applyTransformation(float it, Transformation tr) {
76        float translationT = mTranslateInterpolator.getInterpolation(mNormalizedTime);
77        int translation =
78                (int) (mFromTranslateY + (mToTranslateY - mFromTranslateY) * translationT);
79        Rect oldClipRect = tr.getClipRect();
80        tr.setClipRect(oldClipRect.left,
81                mFromRect.top - translation + (int) ((mToRect.top - mFromRect.top) * it),
82                oldClipRect.right,
83                mFromRect.bottom - translation + (int) ((mToRect.bottom - mFromRect.bottom) * it));
84    }
85
86}
87