ParallaxTarget.java revision 05de4b68e45fc22c867d49ab88e2bdfd599bf7cc
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; 18 19import java.util.List; 20 21/** 22 * ParallaxTarget is reponsible for updating the target through the {@link #update(float)} method. 23 * {@link ParallaxEffect} transforms the values of {@link ParallaxSource}, which represents the 24 * current state of UI, into a float value between 0 and 1. That float value is passed into 25 * {@link #update(float)} method. 26 */ 27public abstract class ParallaxTarget { 28 29 /** 30 * Implementation class is supposed to update target with the provided fraction 31 * (between 0 and 1). The fraction represents percentage of completed change (e.g. scroll) on 32 * target. 33 * 34 * @param fraction Fraction between 0 to 1. 35 */ 36 public abstract void update(float fraction); 37 38 /** 39 * Returns the current fraction (between 0 and 1). The fraction represents percentage of 40 * completed change (e.g. scroll) on target. 41 * 42 * @return Current fraction value. 43 */ 44 public abstract float getFraction(); 45 46 /** 47 * PropertyValuesHolderTarget is an implementation of {@link ParallaxTarget} that uses 48 * {@link PropertyValuesHolder} to update the target object. 49 */ 50 public static final class PropertyValuesHolderTarget extends ParallaxTarget { 51 52 /** 53 * We simulate a parallax effect on target object using an ObjectAnimator. PSEUDO_DURATION 54 * is used on the ObjectAnimator. 55 */ 56 private static final long PSEUDO_DURATION = 1000000; 57 58 private final ObjectAnimator mAnimator; 59 private float mFraction; 60 61 public PropertyValuesHolderTarget(Object targetObject, PropertyValuesHolder values) { 62 mAnimator = ObjectAnimator.ofPropertyValuesHolder(targetObject, values); 63 mAnimator.setDuration(PSEUDO_DURATION); 64 } 65 66 @Override 67 public void update(float fraction) { 68 mFraction = fraction; 69 mAnimator.setCurrentPlayTime((long) (PSEUDO_DURATION * fraction)); 70 } 71 72 @Override 73 public float getFraction() { 74 return mFraction; 75 } 76 } 77} 78