blob: 495d62010fb8f2d858516d689d704ed7eb0974b6 [file] [log] [blame]
John Hofordabb7d132014-02-24 15:21:42 -08001/*
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 */
14package com.android.test.dynamic;
15
16import android.app.Activity;
17import android.content.res.Resources;
18import android.graphics.drawable.VectorDrawable;
19import android.os.Bundle;
ztenghui996f22f2014-11-21 11:32:29 -080020import android.util.AttributeSet;
21import android.util.Log;
22import android.util.Xml;
John Hofordabb7d132014-02-24 15:21:42 -080023import android.widget.TextView;
24import android.widget.Button;
25import android.widget.GridLayout;
26import android.widget.ScrollView;
ztenghui4d24caf2014-05-28 15:02:35 -070027
ztenghui996f22f2014-11-21 11:32:29 -080028import org.xmlpull.v1.XmlPullParser;
29import org.xmlpull.v1.XmlPullParserException;
30
31import java.io.IOException;
John Hofordabb7d132014-02-24 15:21:42 -080032import java.text.DecimalFormat;
33
34@SuppressWarnings({"UnusedDeclaration"})
ztenghui498213a2014-05-05 16:26:22 -070035public class VectorDrawablePerformance extends Activity {
John Hofordabb7d132014-02-24 15:21:42 -080036 private static final String LOGCAT = "VectorDrawable1";
37 protected int[] icon = {
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080038 R.drawable.vector_icon_gradient_1,
39 R.drawable.vector_icon_gradient_2,
40 R.drawable.vector_icon_gradient_3,
Teng-Hui Zhu1664a822016-03-04 15:08:00 -080041 R.drawable.vector_icon_gradient_1_clamp,
42 R.drawable.vector_icon_gradient_2_repeat,
43 R.drawable.vector_icon_gradient_3_mirror,
Teng-Hui Zhu9b85c0b2016-01-28 11:04:48 -080044 R.drawable.vector_icon_state_list_simple,
45 R.drawable.vector_icon_state_list_theme,
John Hofordabb7d132014-02-24 15:21:42 -080046 R.drawable.vector_drawable01,
47 R.drawable.vector_drawable02,
48 R.drawable.vector_drawable03,
49 R.drawable.vector_drawable04,
50 R.drawable.vector_drawable05,
51 R.drawable.vector_drawable06,
52 R.drawable.vector_drawable07,
53 R.drawable.vector_drawable08,
54 R.drawable.vector_drawable09,
55 R.drawable.vector_drawable10,
56 R.drawable.vector_drawable11,
57 R.drawable.vector_drawable12,
58 R.drawable.vector_drawable13,
59 R.drawable.vector_drawable14,
60 R.drawable.vector_drawable15,
61 R.drawable.vector_drawable16,
62 R.drawable.vector_drawable17,
63 R.drawable.vector_drawable18,
64 R.drawable.vector_drawable19,
ztenghui452f6ec2014-05-28 09:48:36 -070065 R.drawable.vector_drawable20,
ztenghui4d24caf2014-05-28 15:02:35 -070066 R.drawable.vector_drawable21,
67 R.drawable.vector_drawable22,
ztenghui2af745b2014-05-30 16:23:47 -070068 R.drawable.vector_drawable23,
69 R.drawable.vector_drawable24,
ztenghui8d0d24f2014-07-09 10:49:44 -070070 R.drawable.vector_drawable25,
ztenghuid1c89b12014-08-06 10:42:21 -070071 R.drawable.vector_drawable26,
72 R.drawable.vector_drawable27,
73 R.drawable.vector_drawable28,
ztenghui4cc3e272014-10-07 13:11:22 -070074 R.drawable.vector_drawable29,
ztenghui5e7a29f2014-11-12 16:19:03 -080075 R.drawable.vector_drawable30,
ztenghui9af77a4c2015-04-01 16:38:44 -070076 R.drawable.vector_drawable_scale0,
77 R.drawable.vector_drawable_scale1,
78 R.drawable.vector_drawable_scale2,
79 R.drawable.vector_drawable_scale3,
John Hofordabb7d132014-02-24 15:21:42 -080080 };
81
ztenghui996f22f2014-11-21 11:32:29 -080082 public static VectorDrawable create(Resources resources, int rid) {
83 try {
84 final XmlPullParser parser = resources.getXml(rid);
85 final AttributeSet attrs = Xml.asAttributeSet(parser);
86 int type;
87 while ((type=parser.next()) != XmlPullParser.START_TAG &&
88 type != XmlPullParser.END_DOCUMENT) {
89 // Empty loop
90 }
91 if (type != XmlPullParser.START_TAG) {
92 throw new XmlPullParserException("No start tag found");
93 }
94
95 final VectorDrawable drawable = new VectorDrawable();
96 drawable.inflate(resources, parser, attrs);
97
98 return drawable;
99 } catch (XmlPullParserException e) {
100 Log.e(LOGCAT, "parser error", e);
101 } catch (IOException e) {
102 Log.e(LOGCAT, "parser error", e);
103 }
104 return null;
105 }
106
John Hofordabb7d132014-02-24 15:21:42 -0800107 @Override
108 protected void onCreate(Bundle savedInstanceState) {
109 super.onCreate(savedInstanceState);
110 ScrollView scrollView = new ScrollView(this);
111 GridLayout container = new GridLayout(this);
112 scrollView.addView(container);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800113 container.setColumnCount(4);
John Hofordabb7d132014-02-24 15:21:42 -0800114 Resources res = this.getResources();
115 container.setBackgroundColor(0xFF888888);
116 VectorDrawable []d = new VectorDrawable[icon.length];
117 long time = android.os.SystemClock.elapsedRealtimeNanos();
118 for (int i = 0; i < icon.length; i++) {
ztenghui996f22f2014-11-21 11:32:29 -0800119 d[i] = create(res,icon[i]);
John Hofordabb7d132014-02-24 15:21:42 -0800120 }
121 time = android.os.SystemClock.elapsedRealtimeNanos()-time;
122 TextView t = new TextView(this);
123 DecimalFormat df = new DecimalFormat("#.##");
124 t.setText("avgL=" + df.format(time / (icon.length * 1000000.)) + " ms");
John Hofordabb7d132014-02-24 15:21:42 -0800125 container.addView(t);
126 time = android.os.SystemClock.elapsedRealtimeNanos();
127 for (int i = 0; i < icon.length; i++) {
128 Button button = new Button(this);
129 button.setWidth(200);
John Hofordabb7d132014-02-24 15:21:42 -0800130 button.setBackgroundResource(icon[i]);
131 container.addView(button);
John Hofordabb7d132014-02-24 15:21:42 -0800132 }
133 setContentView(scrollView);
134 time = android.os.SystemClock.elapsedRealtimeNanos()-time;
135 t = new TextView(this);
136 t.setText("avgS=" + df.format(time / (icon.length * 1000000.)) + " ms");
John Hofordabb7d132014-02-24 15:21:42 -0800137 container.addView(t);
138 }
John Hofordabb7d132014-02-24 15:21:42 -0800139}