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 android.support.v17.leanback.transition;
18
19import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20
21import android.animation.Animator;
22import android.animation.ValueAnimator;
23import android.content.Context;
24import android.support.annotation.RequiresApi;
25import android.support.annotation.RestrictTo;
26import android.support.v17.leanback.R;
27import android.support.v17.leanback.widget.Parallax;
28import android.transition.TransitionValues;
29import android.transition.Visibility;
30import android.util.AttributeSet;
31import android.view.View;
32import android.view.ViewGroup;
33import android.view.animation.Interpolator;
34import android.view.animation.LinearInterpolator;
35
36/**
37 * A slide transition changes TRANSLATION attribute of view which is not exposed by any event.
38 * In order to run a parallax effect with Slide transition, ParallaxTransition is running on the
39 * side, calling ParallaxSource.updateValues() on every frame. User should make sure slide
40 * and ParallaxTransition are using same duration and startDelay.
41 *
42 * @hide
43 */
44@RequiresApi(21)
45@RestrictTo(LIBRARY_GROUP)
46public class ParallaxTransition extends Visibility {
47
48    static Interpolator sInterpolator = new LinearInterpolator();
49
50    public ParallaxTransition() {
51    }
52
53    public ParallaxTransition(Context context, AttributeSet attrs) {
54        super(context, attrs);
55    }
56
57    Animator createAnimator(View view) {
58        final Parallax source = (Parallax) view.getTag(R.id.lb_parallax_source);
59        if (source == null) {
60            return null;
61        }
62        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
63        animator.setInterpolator(sInterpolator);
64        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
65            @Override
66            public void onAnimationUpdate(ValueAnimator animation) {
67                source.updateValues();
68            }
69        });
70        return animator;
71    }
72
73    @Override
74    public Animator onAppear(ViewGroup sceneRoot, View view,
75                             TransitionValues startValues, TransitionValues endValues) {
76        if (endValues == null) {
77            return null;
78        }
79        return createAnimator(view);
80    }
81
82    @Override
83    public Animator onDisappear(ViewGroup sceneRoot, View view,
84                                TransitionValues startValues, TransitionValues endValues) {
85        if (startValues == null) {
86            return null;
87        }
88        return createAnimator(view);
89    }
90}
91