AnimatedVectorDrawableTest.java revision 9ac5a3333db48e169d2e89e1e9062ed943d20589
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.android.test.dynamic;
16
17import android.app.Activity;
18import android.graphics.drawable.Animatable2;
19import android.graphics.drawable.AnimatedVectorDrawable;
20import android.graphics.drawable.Drawable;
21import android.os.Bundle;
22import android.util.Log;
23import android.view.View;
24import android.widget.Button;
25import android.widget.GridLayout;
26import android.widget.ScrollView;
27import android.widget.TextView;
28
29public class AnimatedVectorDrawableTest extends Activity implements View.OnClickListener {
30    private static final String LOGCAT = "AnimatedVectorDrawableTest";
31
32    protected int[] icon = {
33            R.drawable.btn_radio_on_to_off_bundle,
34            R.drawable.ic_rotate_2_portrait_v2_animation,
35            R.drawable.ic_signal_airplane_v2_animation,
36            R.drawable.ic_hourglass_animation,
37            R.drawable.animation_vector_linear_progress_bar,
38            R.drawable.animation_vector_drawable_grouping_1,
39            R.drawable.animation_vector_progress_bar,
40            R.drawable.animation_vector_drawable_favorite,
41            R.drawable.animation_vector_drawable01,
42            // Duplicate to test constant state.
43            R.drawable.animation_vector_drawable01,
44    };
45
46    @Override
47    protected void onCreate(Bundle savedInstanceState) {
48        final int[] layerTypes = {View.LAYER_TYPE_SOFTWARE, View.LAYER_TYPE_HARDWARE};
49        final boolean[] forceOnUi = {false, true};
50        super.onCreate(savedInstanceState);
51
52        ScrollView scrollView = new ScrollView(this);
53        GridLayout container = new GridLayout(this);
54        scrollView.addView(container);
55        container.setColumnCount(layerTypes.length * forceOnUi.length);
56
57        for (int j = 0; j < layerTypes.length; j++) {
58            for (int k = 0; k < forceOnUi.length; k++) {
59                TextView textView = new TextView(this);
60                String category = "Layer:"
61                        + (layerTypes[j] == View.LAYER_TYPE_SOFTWARE ? "SW" : "HW")
62                        + (forceOnUi[k] == true ? ",forceUI" : "");
63                textView.setText(category);
64                container.addView(textView);
65            }
66        }
67        for (int i = 0; i < icon.length; i++) {
68            for (int j = 0; j < layerTypes.length; j++) {
69                for (int k = 0; k < forceOnUi.length; k++) {
70                    Button button = new Button(this);
71                    button.setWidth(300);
72                    button.setHeight(300);
73                    button.setLayerType(layerTypes[j], null);
74                    button.setBackgroundResource(icon[i]);
75                    AnimatedVectorDrawable d = (AnimatedVectorDrawable) button.getBackground();
76                    if (forceOnUi[k] == true) {
77                        d.forceAnimationOnUI();
78                    }
79                    d.registerAnimationCallback(new Animatable2.AnimationCallback() {
80                        @Override
81                        public void onAnimationStart(Drawable drawable) {
82                            Log.v(LOGCAT, "Animator start");
83                        }
84
85                        @Override
86                        public void onAnimationEnd(Drawable drawable) {
87                            Log.v(LOGCAT, "Animator end");
88                        }
89                    });
90
91                    container.addView(button);
92                    button.setOnClickListener(this);
93                }
94            }
95        }
96
97        setContentView(scrollView);
98    }
99
100    @Override
101    public void onClick(View v) {
102        AnimatedVectorDrawable d = (AnimatedVectorDrawable) v.getBackground();
103        d.start();
104    }
105}
106