ChangeScroll.java revision 2dcab18c6c9381122df6e06a93912e53dff69408
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
17package android.transition;
18
19import android.animation.Animator;
20import android.animation.ObjectAnimator;
21import android.content.Context;
22import android.transition.Transition;
23import android.transition.TransitionValues;
24import android.util.AttributeSet;
25import android.view.View;
26import 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 */
34public 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
39    public ChangeScroll() {}
40
41    public ChangeScroll(Context context, AttributeSet attrs) {
42        super(context, attrs);
43    }
44
45    @Override
46    public void captureStartValues(TransitionValues transitionValues) {
47        captureValues(transitionValues);
48    }
49
50    @Override
51    public void captureEndValues(TransitionValues transitionValues) {
52        captureValues(transitionValues);
53    }
54
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
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