Home | History | Annotate | Download | only in dynamic
      1 /*
      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  */
     14 package com.android.test.dynamic;
     15 
     16 import android.app.Activity;
     17 import android.content.res.Resources;
     18 import android.graphics.drawable.VectorDrawable;
     19 import android.os.Bundle;
     20 import android.util.AttributeSet;
     21 import android.util.Log;
     22 import android.util.Xml;
     23 import android.widget.TextView;
     24 import android.widget.Button;
     25 import android.widget.GridLayout;
     26 import android.widget.ScrollView;
     27 
     28 import org.xmlpull.v1.XmlPullParser;
     29 import org.xmlpull.v1.XmlPullParserException;
     30 
     31 import java.io.IOException;
     32 import java.text.DecimalFormat;
     33 
     34 @SuppressWarnings({"UnusedDeclaration"})
     35 public class VectorDrawablePerformance extends Activity {
     36     private static final String LOGCAT = "VectorDrawable1";
     37     protected int[] icon = {
     38             R.drawable.vector_icon_filltype_nonzero,
     39             R.drawable.vector_icon_filltype_evenodd,
     40             R.drawable.vector_icon_gradient_1,
     41             R.drawable.vector_icon_gradient_2,
     42             R.drawable.vector_icon_gradient_3,
     43             R.drawable.vector_icon_gradient_1_clamp,
     44             R.drawable.vector_icon_gradient_2_repeat,
     45             R.drawable.vector_icon_gradient_3_mirror,
     46             R.drawable.vector_icon_state_list_simple,
     47             R.drawable.vector_icon_state_list_theme,
     48             R.drawable.vector_drawable01,
     49             R.drawable.vector_drawable02,
     50             R.drawable.vector_drawable03,
     51             R.drawable.vector_drawable04,
     52             R.drawable.vector_drawable05,
     53             R.drawable.vector_drawable06,
     54             R.drawable.vector_drawable07,
     55             R.drawable.vector_drawable08,
     56             R.drawable.vector_drawable09,
     57             R.drawable.vector_drawable10,
     58             R.drawable.vector_drawable11,
     59             R.drawable.vector_drawable12,
     60             R.drawable.vector_drawable13,
     61             R.drawable.vector_drawable14,
     62             R.drawable.vector_drawable15,
     63             R.drawable.vector_drawable16,
     64             R.drawable.vector_drawable17,
     65             R.drawable.vector_drawable18,
     66             R.drawable.vector_drawable19,
     67             R.drawable.vector_drawable20,
     68             R.drawable.vector_drawable21,
     69             R.drawable.vector_drawable22,
     70             R.drawable.vector_drawable23,
     71             R.drawable.vector_drawable24,
     72             R.drawable.vector_drawable25,
     73             R.drawable.vector_drawable26,
     74             R.drawable.vector_drawable27,
     75             R.drawable.vector_drawable28,
     76             R.drawable.vector_drawable29,
     77             R.drawable.vector_drawable30,
     78             R.drawable.vector_drawable_scale0,
     79             R.drawable.vector_drawable_scale1,
     80             R.drawable.vector_drawable_scale2,
     81             R.drawable.vector_drawable_scale3,
     82     };
     83 
     84     public static VectorDrawable create(Resources resources, int rid) {
     85         try {
     86             final XmlPullParser parser = resources.getXml(rid);
     87             final AttributeSet attrs = Xml.asAttributeSet(parser);
     88             int type;
     89             while ((type=parser.next()) != XmlPullParser.START_TAG &&
     90                     type != XmlPullParser.END_DOCUMENT) {
     91                 // Empty loop
     92             }
     93             if (type != XmlPullParser.START_TAG) {
     94                 throw new XmlPullParserException("No start tag found");
     95             }
     96 
     97             final VectorDrawable drawable = new VectorDrawable();
     98             drawable.inflate(resources, parser, attrs);
     99 
    100             return drawable;
    101         } catch (XmlPullParserException e) {
    102             Log.e(LOGCAT, "parser error", e);
    103         } catch (IOException e) {
    104             Log.e(LOGCAT, "parser error", e);
    105         }
    106         return null;
    107     }
    108 
    109     @Override
    110     protected void onCreate(Bundle savedInstanceState) {
    111         super.onCreate(savedInstanceState);
    112         ScrollView scrollView = new ScrollView(this);
    113         GridLayout container = new GridLayout(this);
    114         scrollView.addView(container);
    115         container.setColumnCount(4);
    116         Resources res = this.getResources();
    117         container.setBackgroundColor(0xFF888888);
    118         VectorDrawable []d = new VectorDrawable[icon.length];
    119         long time =  android.os.SystemClock.elapsedRealtimeNanos();
    120         for (int i = 0; i < icon.length; i++) {
    121              d[i] = create(res,icon[i]);
    122         }
    123         time =  android.os.SystemClock.elapsedRealtimeNanos()-time;
    124         TextView t = new TextView(this);
    125         DecimalFormat df = new DecimalFormat("#.##");
    126         t.setText("avgL=" + df.format(time / (icon.length * 1000000.)) + " ms");
    127         container.addView(t);
    128         time =  android.os.SystemClock.elapsedRealtimeNanos();
    129         for (int i = 0; i < icon.length; i++) {
    130             Button button = new Button(this);
    131             button.setWidth(200);
    132             button.setBackgroundResource(icon[i]);
    133             container.addView(button);
    134         }
    135         setContentView(scrollView);
    136         time =  android.os.SystemClock.elapsedRealtimeNanos()-time;
    137         t = new TextView(this);
    138         t.setText("avgS=" + df.format(time / (icon.length * 1000000.)) + " ms");
    139         container.addView(t);
    140     }
    141 }
    142