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.content.res.Resources;
20 import android.graphics.PorterDuff.Mode;
21 import android.graphics.drawable.Drawable.ConstantState;
22 import android.os.Bundle;
23 import android.widget.Button;
24 import android.widget.LinearLayout;
25 import android.widget.ScrollView;
26 import android.widget.TextView;
27 
28 import androidx.appcompat.app.AppCompatActivity;
29 import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
30 
31 import com.example.android.support.vectordrawable.R;
32 
33 import java.text.DecimalFormat;
34 
35 /**
36  * Simple demo for VectorDrawableCompat.
37  */
38 public class SimpleStaticVectorDrawable extends AppCompatActivity {
39     protected int[] mIcons = {
40             R.drawable.vector_drawable_scale0,
41             R.drawable.vector_drawable_scale1,
42             R.drawable.vector_drawable_scale2,
43             R.drawable.vector_drawable_scale3,
44             R.drawable.vector_drawable01,
45             R.drawable.vector_drawable02,
46             R.drawable.vector_drawable03,
47             R.drawable.vector_drawable04,
48             R.drawable.vector_drawable05,
49             R.drawable.vector_drawable06,
50             R.drawable.vector_drawable07,
51             R.drawable.vector_drawable08,
52             R.drawable.vector_drawable09,
53             R.drawable.vector_drawable10,
54             R.drawable.vector_drawable11,
55             R.drawable.vector_drawable12,
56             R.drawable.vector_drawable13,
57             R.drawable.vector_drawable14,
58             R.drawable.vector_drawable15,
59             R.drawable.vector_drawable16,
60             R.drawable.vector_drawable17,
61             R.drawable.vector_drawable18,
62             R.drawable.vector_drawable19,
63             R.drawable.vector_drawable20,
64             R.drawable.vector_drawable21,
65             R.drawable.vector_drawable22,
66             R.drawable.vector_drawable23,
67             R.drawable.vector_drawable24,
68             R.drawable.vector_drawable25,
69             R.drawable.vector_drawable26,
70             R.drawable.vector_drawable27,
71             R.drawable.vector_drawable28,
72             R.drawable.vector_drawable29,
73             R.drawable.vector_drawable30,
74             R.drawable.vector_test01,
75             R.drawable.vector_test02,
76             R.drawable.vector_icon_gradient_1,
77             R.drawable.vector_icon_gradient_2,
78             R.drawable.vector_icon_gradient_3,
79             R.drawable.vector_icon_state_list
80     };
81 
82     private static final int EXTRA_TESTS = 2;
83 
84     @Override
onCreate(Bundle savedInstanceState)85     protected void onCreate(Bundle savedInstanceState) {
86         super.onCreate(savedInstanceState);
87         ScrollView scrollView = new ScrollView(this);
88         LinearLayout container = new LinearLayout(this);
89         scrollView.addView(container);
90         container.setOrientation(LinearLayout.VERTICAL);
91         final Resources res = getResources();
92         final Resources.Theme theme = getTheme();
93         container.setBackgroundColor(0xFF888888);
94         VectorDrawableCompat[] d = new VectorDrawableCompat[mIcons.length];
95         long time = android.os.SystemClock.currentThreadTimeMillis();
96         for (int i = 0; i < mIcons.length; i++) {
97             d[i] = VectorDrawableCompat.create(res, mIcons[i], theme);
98         }
99         time = android.os.SystemClock.currentThreadTimeMillis() - time;
100 
101         // Testing Tint on one particular case.
102         if (d.length > 3) {
103             d[3].setTint(0x8000FF00);
104             d[3].setTintMode(Mode.MULTIPLY);
105         }
106 
107         // Testing Constant State like operation by creating the first 2 icons
108         // from the 3rd one's constant state.
109         VectorDrawableCompat[] extras = new VectorDrawableCompat[EXTRA_TESTS];
110         ConstantState state = d[0].getConstantState();
111         extras[0] = (VectorDrawableCompat) state.newDrawable();
112         extras[1] = (VectorDrawableCompat) state.newDrawable();
113 
114         // This alpha change is expected to affect both extra 0, 1, and d0.
115         extras[0].setAlpha(128);
116 
117         d[0].mutate();
118         d[0].setAlpha(255);
119 
120         // Just show the average create time as the first view.
121         TextView t = new TextView(this);
122         DecimalFormat df = new DecimalFormat("#.##");
123         t.setText("avgL=" + df.format(time / mIcons.length) + " ms");
124         container.addView(t);
125 
126         addDrawableButtons(container, extras);
127 
128         addDrawableButtons(container, d);
129 
130         setContentView(scrollView);
131     }
132 
addDrawableButtons(LinearLayout container, VectorDrawableCompat[] d)133     private void addDrawableButtons(LinearLayout container, VectorDrawableCompat[] d) {
134         // Add the VD into consequent views.
135         for (int i = 0; i < d.length; i++) {
136             Button button = new Button(this);
137             button.setWidth(200);
138             // Note that setBackgroundResource() will fail b/c createFromXmlInner() failed
139             // to recognize <vector> pre-L.
140             button.setBackgroundDrawable(d[i]);
141             container.addView(button);
142         }
143     }
144 }
145