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 */
16package android.support.v17.leanback.transition;
17
18import android.animation.Animator;
19import android.animation.ValueAnimator;
20import android.view.View;
21import android.view.ViewGroup;
22import android.transition.Transition;
23import android.transition.TransitionValues;
24
25/**
26 * @hide
27 */
28class Scale extends Transition {
29    private static final String PROPNAME_SCALE = "android:leanback:scale";
30
31    public Scale() {
32    }
33
34    private void captureValues(TransitionValues values) {
35        View view = values.view;
36        values.values.put(PROPNAME_SCALE, view.getScaleX());
37    }
38
39    @Override
40    public void captureStartValues(TransitionValues transitionValues) {
41        captureValues(transitionValues);
42    }
43
44    @Override
45    public void captureEndValues(TransitionValues transitionValues) {
46        captureValues(transitionValues);
47    }
48
49    @Override
50    public Animator createAnimator(final ViewGroup sceneRoot, TransitionValues startValues,
51            TransitionValues endValues) {
52        if (startValues == null || endValues == null) {
53            return null;
54        }
55
56        final float startScale = (Float) startValues.values.get(PROPNAME_SCALE);
57        final float endScale = (Float) endValues.values.get(PROPNAME_SCALE);
58
59        final View view = startValues.view;
60        view.setScaleX(startScale);
61        view.setScaleY(startScale);
62
63        ValueAnimator animator = ValueAnimator.ofFloat(startScale, endScale);
64        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
65            @Override
66            public void onAnimationUpdate(ValueAnimator animation) {
67                final float scale = (Float) animation.getAnimatedValue();
68                view.setScaleX(scale);
69                view.setScaleY(scale);
70            }
71        });
72        return animator;
73    }
74}
75