1 /*
2  * Copyright (C) 2014 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 
17 package com.android.tv.settings.widget;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorSet;
21 import android.animation.ValueAnimator;
22 import android.content.Context;
23 import android.util.DisplayMetrics;
24 import android.util.TypedValue;
25 import android.view.View;
26 
27 import java.util.ArrayList;
28 
29 /**
30  * SimpleScrollAdapterTransform is the default implementation of {@link ScrollAdapterTransform} used
31  * by ScrollAdapterView. It uses two Animator objects to transform views.
32  */
33 public class SimpleScrollAdapterTransform implements ScrollAdapterTransform {
34 
35     /** Animator for transform views on the right/down side of mScrollCenter */
36     private Animator mHighItemTransform;
37 
38     /** for transform views on the left/up side of mScrollCenter */
39     private Animator mLowItemTransform;
40 
41     private final DisplayMetrics mDisplayMetrics;
42 
SimpleScrollAdapterTransform(Context context)43     public SimpleScrollAdapterTransform(Context context) {
44         mDisplayMetrics = context.getResources().getDisplayMetrics();
45     }
46 
47     @Override
transform(View child, int distanceFromCenter, int distanceFromCenter2ndAxis)48     public void transform(View child, int distanceFromCenter, int distanceFromCenter2ndAxis) {
49         if (mLowItemTransform == null && mHighItemTransform == null) {
50             return;
51         }
52         int absDistance = Math.abs(distanceFromCenter) + Math.abs(distanceFromCenter2ndAxis);
53         if (distanceFromCenter < 0) {
54             applyTransformationRecursive(absDistance, mLowItemTransform, child);
55         } else {
56             applyTransformationRecursive(absDistance, mHighItemTransform, child);
57         }
58     }
59 
applyTransformationRecursive( int distanceFromCenter, Animator animator, View child)60     private void applyTransformationRecursive(
61             int distanceFromCenter, Animator animator, View child) {
62         if (animator instanceof AnimatorSet) {
63             ArrayList<Animator> children = ((AnimatorSet) animator).getChildAnimations();
64             for (int i = children.size() - 1; i >= 0; i--) {
65                 applyTransformationRecursive(distanceFromCenter, children.get(i), child);
66             }
67         } else if (animator instanceof ValueAnimator) {
68             ValueAnimator valueAnim = ((ValueAnimator) animator);
69             valueAnim.setTarget(child);
70             long duration = valueAnim.getDuration();
71             if (distanceFromCenter < duration) {
72                 valueAnim.setCurrentPlayTime(distanceFromCenter);
73             } else {
74                 valueAnim.setCurrentPlayTime(duration);
75             }
76         }
77     }
78 
initializeTransformationRecursive(Animator animator, long defaultDuration)79     private void initializeTransformationRecursive(Animator animator, long defaultDuration) {
80         long duration = animator.getDuration();
81         if (duration == 0) {
82             duration = defaultDuration;
83         }
84         if (animator instanceof AnimatorSet) {
85             ArrayList<Animator> children = ((AnimatorSet) animator).getChildAnimations();
86             for (int i = children.size() - 1; i >= 0; i--) {
87                 initializeTransformationRecursive(children.get(i), duration);
88             }
89         } else if (animator instanceof ValueAnimator) {
90             ValueAnimator valueAnim = ((ValueAnimator) animator);
91             // DIP to pixel, save back to duration
92             valueAnim.setDuration((long) TypedValue.applyDimension(
93                     TypedValue.COMPLEX_UNIT_DIP, duration, mDisplayMetrics));
94         }
95     }
96 
getHighItemTransform()97     public Animator getHighItemTransform() {
98         return mHighItemTransform;
99     }
100 
setHighItemTransform(Animator highItemTransform)101     public void setHighItemTransform(Animator highItemTransform) {
102         mHighItemTransform = highItemTransform;
103         initializeTransformationRecursive(mHighItemTransform, 0);
104     }
105 
getLowItemTransform()106     public Animator getLowItemTransform() {
107         return mLowItemTransform;
108     }
109 
setLowItemTransform(Animator lowItemTransform)110     public void setLowItemTransform(Animator lowItemTransform) {
111         mLowItemTransform = lowItemTransform;
112         initializeTransformationRecursive(mLowItemTransform, 0);
113     }
114 
115 }
116