1 /*
2  * Copyright (C) 2015 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 
17 package com.example.android.support.vectordrawable.app;
18 
19 import android.app.Activity;
20 import android.content.res.Resources;
21 import android.os.Bundle;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.LinearLayout;
25 import android.widget.ScrollView;
26 import android.widget.TextView;
27 
28 import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
29 
30 import com.example.android.support.vectordrawable.R;
31 
32 import java.text.DecimalFormat;
33 
34 /**
35  * Simple demo for AnimatedVectorDrawableCompat.
36  */
37 public class SimpleAnimatedVectorDrawable extends Activity implements View.OnClickListener {
38     private static final String LOG_TAG = "TestActivity";
39 
40     private static final String LOGCAT = "VectorDrawable1";
41     protected int[] mIcons = {
42             R.drawable.animation_vector_drawable_grouping_1_path_motion,
43             R.drawable.animation_vector_drawable_grouping_1_path_motion_object,
44             R.drawable.animation_vector_drawable_grouping_1,
45             R.drawable.animation_vector_drawable_grouping_decelerate,
46             R.drawable.animation_vector_drawable_grouping_accelerate,
47             R.drawable.ic_hourglass_animation,
48             R.drawable.ic_signal_airplane_v2_animation,
49             R.drawable.animation_vector_progress_bar,
50             R.drawable.btn_radio_on_to_off_bundle,
51     };
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         ScrollView scrollView = new ScrollView(this);
57         LinearLayout container = new LinearLayout(this);
58         scrollView.addView(container);
59         container.setOrientation(LinearLayout.VERTICAL);
60         Resources res = this.getResources();
61         container.setBackgroundColor(0xFF888888);
62         AnimatedVectorDrawableCompat[] d = new AnimatedVectorDrawableCompat[mIcons.length];
63         long time = android.os.SystemClock.currentThreadTimeMillis();
64         for (int i = 0; i < mIcons.length; i++) {
65             d[i] = AnimatedVectorDrawableCompat.create(this, mIcons[i]);
66         }
67         time = android.os.SystemClock.currentThreadTimeMillis() - time;
68         TextView t = new TextView(this);
69         DecimalFormat df = new DecimalFormat("#.##");
70         t.setText("avgL=" + df.format(time / (mIcons.length)) + " ms");
71         container.addView(t);
72 
73         addDrawableButtons(container, d);
74 
75         // Now test constant state and mutate a bit.
76         if (d[0].getConstantState() != null) {
77             AnimatedVectorDrawableCompat[] copies = new AnimatedVectorDrawableCompat[3];
78             copies[0] = (AnimatedVectorDrawableCompat) d[0].getConstantState().newDrawable();
79             copies[1] = (AnimatedVectorDrawableCompat) d[0].getConstantState().newDrawable();
80             copies[2] = (AnimatedVectorDrawableCompat) d[0].getConstantState().newDrawable();
81             copies[0].setAlpha(128);
82 
83             // Expect to see the copies[0, 1] are showing alpha 128, and [2] are showing 255.
84             copies[2].mutate();
85             copies[2].setAlpha(255);
86 
87             addDrawableButtons(container, copies);
88         }
89 
90         setContentView(scrollView);
91     }
92 
addDrawableButtons(LinearLayout container, AnimatedVectorDrawableCompat[] d)93     private void addDrawableButtons(LinearLayout container, AnimatedVectorDrawableCompat[] d) {
94         for (int i = 0; i < d.length; i++) {
95             Button button = new Button(this);
96             button.setWidth(200);
97             button.setHeight(200);
98             button.setBackgroundDrawable(d[i]);
99             container.addView(button);
100             button.setOnClickListener(this);
101         }
102     }
103 
104     @Override
onClick(View v)105     public void onClick(View v) {
106         AnimatedVectorDrawableCompat d = (AnimatedVectorDrawableCompat) v.getBackground();
107         d.start();
108     }
109 }
110