• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.transition;
18 
19 import android.animation.Animator;
20 import android.animation.ObjectAnimator;
21 import android.content.Context;
22 import android.transition.Transition;
23 import android.transition.TransitionValues;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 /**
29  * This transition captures the scroll properties of targets before and after
30  * the scene change and animates any changes.
31  *
32  * @hide
33  */
34 public class ChangeScroll extends Transition {
35 
36     private static final String PROPNAME_SCROLL_X = "android:changeScroll:x";
37     private static final String PROPNAME_SCROLL_Y = "android:changeScroll:y";
38 
ChangeScroll()39     public ChangeScroll() {}
40 
ChangeScroll(Context context, AttributeSet attrs)41     public ChangeScroll(Context context, AttributeSet attrs) {
42         super(context, attrs);
43     }
44 
45     @Override
captureStartValues(TransitionValues transitionValues)46     public void captureStartValues(TransitionValues transitionValues) {
47         captureValues(transitionValues);
48     }
49 
50     @Override
captureEndValues(TransitionValues transitionValues)51     public void captureEndValues(TransitionValues transitionValues) {
52         captureValues(transitionValues);
53     }
54 
captureValues(TransitionValues transitionValues)55     private void captureValues(TransitionValues transitionValues) {
56         transitionValues.values.put(PROPNAME_SCROLL_X, transitionValues.view.getScrollX());
57         transitionValues.values.put(PROPNAME_SCROLL_Y, transitionValues.view.getScrollY());
58     }
59 
60     @Override
createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues)61     public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
62             TransitionValues endValues) {
63         if (startValues == null || endValues == null) {
64             return null;
65         }
66         final View view = endValues.view;
67         int startX = (Integer) startValues.values.get(PROPNAME_SCROLL_X);
68         int endX = (Integer) endValues.values.get(PROPNAME_SCROLL_X);
69         int startY = (Integer) startValues.values.get(PROPNAME_SCROLL_Y);
70         int endY = (Integer) endValues.values.get(PROPNAME_SCROLL_Y);
71         Animator scrollXAnimator = null;
72         Animator scrollYAnimator = null;
73         if (startX != endX) {
74             view.setScrollX(startX);
75             scrollXAnimator = ObjectAnimator.ofInt(view, "scrollX", startX, endX);
76         }
77         if (startY != endY) {
78             view.setScrollY(startY);
79             scrollYAnimator = ObjectAnimator.ofInt(view, "scrollY", startY, endY);
80         }
81         return TransitionUtils.mergeAnimators(scrollXAnimator, scrollYAnimator);
82     }
83 }
84