ParallaxTarget.java revision 46ae6eb27de10f019258fe197c794cf2234abe5d
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.widget;
15
16import android.animation.ObjectAnimator;
17import android.animation.PropertyValuesHolder;
18import android.view.animation.LinearInterpolator;
19
20import java.util.List;
21
22/**
23 * ParallaxTarget is reponsible for updating the target through the {@link #update(float)} method.
24 * {@link ParallaxEffect} transforms the values of {@link ParallaxSource}, which represents the
25 * current state of UI, into a float value between 0 and 1. That float value is passed into
26 * {@link #update(float)} method.
27 */
28public abstract class ParallaxTarget {
29
30    /**
31     * Implementation class is supposed to update target with the provided fraction
32     * (between 0 and 1). The fraction represents percentage of completed change (e.g. scroll) on
33     * target.
34     *
35     * @param fraction Fraction between 0 to 1.
36     */
37    public abstract void update(float fraction);
38
39    /**
40     * Returns the current fraction (between 0 and 1). The fraction represents percentage of
41     * completed change (e.g. scroll) on target.
42     *
43     * @return Current fraction value.
44     */
45    public abstract float getFraction();
46
47    /**
48     * PropertyValuesHolderTarget is an implementation of {@link ParallaxTarget} that uses
49     * {@link PropertyValuesHolder} to update the target object.
50     */
51    public static final class PropertyValuesHolderTarget extends ParallaxTarget {
52
53        /**
54         * We simulate a parallax effect on target object using an ObjectAnimator. PSEUDO_DURATION
55         * is used on the ObjectAnimator.
56         */
57        private static final long PSEUDO_DURATION = 1000000;
58
59        private final ObjectAnimator mAnimator;
60        private float mFraction;
61
62        public PropertyValuesHolderTarget(Object targetObject, PropertyValuesHolder values) {
63            mAnimator = ObjectAnimator.ofPropertyValuesHolder(targetObject, values);
64            mAnimator.setInterpolator(new LinearInterpolator());
65            mAnimator.setDuration(PSEUDO_DURATION);
66        }
67
68        @Override
69        public void update(float fraction) {
70            mFraction = fraction;
71            mAnimator.setCurrentPlayTime((long) (PSEUDO_DURATION * fraction));
72        }
73
74        @Override
75        public float getFraction() {
76            return mFraction;
77        }
78    }
79}
80