1/*
2 * Copyright (C) 2016 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.support.design.internal;
18
19import android.animation.Animator;
20import android.animation.ValueAnimator;
21import android.support.annotation.RequiresApi;
22import android.support.annotation.RestrictTo;
23import android.support.transition.Transition;
24import android.support.transition.TransitionValues;
25import android.view.ViewGroup;
26import android.widget.TextView;
27
28import java.util.Map;
29
30/**
31 * @hide
32 */
33@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
34@RequiresApi(14)
35public class TextScale extends Transition {
36    private static final String PROPNAME_SCALE = "android:textscale:scale";
37
38    @Override
39    public void captureStartValues(TransitionValues transitionValues) {
40        captureValues(transitionValues);
41    }
42
43    @Override
44    public void captureEndValues(TransitionValues transitionValues) {
45        captureValues(transitionValues);
46    }
47
48    private void captureValues(TransitionValues transitionValues) {
49        if (transitionValues.view instanceof TextView) {
50            TextView textview = (TextView) transitionValues.view;
51            transitionValues.values.put(PROPNAME_SCALE, textview.getScaleX());
52        }
53    }
54
55    @Override
56    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
57            TransitionValues endValues) {
58        if (startValues == null || endValues == null || !(startValues.view instanceof TextView)
59                || !(endValues.view instanceof TextView)) {
60            return null;
61        }
62        final TextView view = (TextView) endValues.view;
63        Map<String, Object> startVals = startValues.values;
64        Map<String, Object> endVals = endValues.values;
65        final float startSize = startVals.get(PROPNAME_SCALE) != null ? (float) startVals.get(
66                PROPNAME_SCALE) : 1f;
67        final float endSize = endVals.get(PROPNAME_SCALE) != null ? (float) endVals.get(
68                PROPNAME_SCALE) : 1f;
69        if (startSize == endSize) {
70            return null;
71        }
72
73        ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize);
74
75        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
76            @Override
77            public void onAnimationUpdate(ValueAnimator valueAnimator) {
78                float animatedValue = (float) valueAnimator.getAnimatedValue();
79                view.setScaleX(animatedValue);
80                view.setScaleY(animatedValue);
81            }
82        });
83        return animator;
84    }
85}
86