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