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