AnimatedVectorDrawableTest.java revision 5339428d22fb477c79348fde7c1288a7b22e6113
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;
27
28public class AnimatedVectorDrawableTest extends Activity implements View.OnClickListener {
29    private static final String LOGCAT = "AnimatedVectorDrawableTest";
30
31    protected int[] icon = {
32            R.drawable.btn_radio_on_to_off_bundle,
33            R.drawable.ic_rotate_2_portrait_v2_animation,
34            R.drawable.ic_signal_airplane_v2_animation,
35            R.drawable.ic_hourglass_animation,
36            R.drawable.animation_vector_linear_progress_bar,
37            R.drawable.animation_vector_drawable_grouping_1,
38            R.drawable.animation_vector_progress_bar,
39            R.drawable.animation_vector_drawable_favorite,
40            R.drawable.animation_vector_drawable01,
41            // Duplicate to test constant state.
42            R.drawable.animation_vector_drawable01,
43    };
44
45    @Override
46    protected void onCreate(Bundle savedInstanceState) {
47        final int[] layerTypes = {View.LAYER_TYPE_SOFTWARE, View.LAYER_TYPE_HARDWARE};
48        super.onCreate(savedInstanceState);
49
50        ScrollView scrollView = new ScrollView(this);
51        GridLayout container = new GridLayout(this);
52        scrollView.addView(container);
53        container.setColumnCount(2);
54
55        for (int i = 0; i < icon.length; i++) {
56            for (int j = 0; j < layerTypes.length; j++) {
57                Button button = new Button(this);
58                button.setWidth(400);
59                button.setHeight(400);
60                button.setLayerType(layerTypes[j], null);
61                button.setBackgroundResource(icon[i]);
62                AnimatedVectorDrawable d = (AnimatedVectorDrawable) button.getBackground();
63                d.registerAnimationCallback(new Animatable2.AnimationCallback() {
64                    @Override
65                    public void onAnimationStart(Drawable drawable) {
66                        Log.v(LOGCAT, "Animator start");
67                    }
68
69                    @Override
70                    public void onAnimationEnd(Drawable drawable) {
71                        Log.v(LOGCAT, "Animator end");
72                    }
73                });
74
75                container.addView(button);
76                button.setOnClickListener(this);
77            }
78        }
79
80        setContentView(scrollView);
81    }
82
83    @Override
84    public void onClick(View v) {
85        AnimatedVectorDrawable d = (AnimatedVectorDrawable) v.getBackground();
86        d.start();
87    }
88}
89