TrackingTransition.java revision b31c3281d870e9abb673db239234d580dcc4feff
1/*
2 * Copyright 2018 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 */
16package androidx.fragment.app;
17
18import android.animation.Animator;
19import android.graphics.Rect;
20import android.transition.Transition;
21import android.transition.TransitionValues;
22import android.view.View;
23import android.view.ViewGroup;
24
25import java.util.ArrayList;
26
27/**
28 * A transition that tracks which targets are applied to it.
29 * It will assume any target that it applies to will have differences
30 * between the start and end state, regardless of the differences
31 * that actually exist. In other words, it doesn't actually check
32 * any size or position differences or any other property of the view.
33 * It just records the difference.
34 * <p>
35 * Both start and end value Views are recorded, but no actual animation
36 * is created.
37 */
38class TrackingTransition extends Transition implements TargetTracking {
39    public final ArrayList<View> targets = new ArrayList<>();
40    private final Rect[] mEpicenter = new Rect[1];
41    private static final String PROP = "tracking:prop";
42    private static final String[] PROPS = { PROP };
43
44    @Override
45    public String[] getTransitionProperties() {
46        return PROPS;
47    }
48
49    @Override
50    public void captureStartValues(TransitionValues transitionValues) {
51        transitionValues.values.put(PROP, 0);
52    }
53
54    @Override
55    public void captureEndValues(TransitionValues transitionValues) {
56        transitionValues.values.put(PROP, 1);
57    }
58
59    @Override
60    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
61            TransitionValues endValues) {
62        if (startValues != null) {
63            targets.add(startValues.view);
64        }
65        if (endValues != null) {
66            targets.add(endValues.view);
67        }
68        Rect epicenter = getEpicenter();
69        if (epicenter != null) {
70            mEpicenter[0] = new Rect(epicenter);
71        } else {
72            mEpicenter[0] = null;
73        }
74        return null;
75    }
76
77    @Override
78    public ArrayList<View> getTrackedTargets() {
79        return targets;
80    }
81
82    @Override
83    public void clearTargets() {
84        targets.clear();
85    }
86
87    @Override
88    public Rect getCapturedEpicenter() {
89        return mEpicenter[0];
90    }
91}
92