180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui/*
280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * Copyright (C) 2014 The Android Open Source Project
380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui *
480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * in compliance with the License. You may obtain a copy of the License at
680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui *
780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * http://www.apache.org/licenses/LICENSE-2.0
880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui *
980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * Unless required by applicable law or agreed to in writing, software distributed under the License
1080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * or implied. See the License for the specific language governing permissions and limitations under
1280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui * the License.
1380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui */
1480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuipackage com.android.test.dynamic;
1580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
1680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.app.Activity;
1780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.content.res.Resources;
1880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.graphics.drawable.AnimatedVectorDrawable;
1980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.graphics.drawable.VectorDrawable;
2080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.os.Bundle;
2180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.util.AttributeSet;
2280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.util.Log;
2380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.util.Xml;
2480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.widget.Button;
2580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.widget.GridLayout;
2680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.widget.ScrollView;
2780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport android.widget.TextView;
2880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
2980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport org.xmlpull.v1.XmlPullParser;
3080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport org.xmlpull.v1.XmlPullParserException;
3180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
3280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport java.io.IOException;
3380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuiimport java.text.DecimalFormat;
3480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
3580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
3680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui@SuppressWarnings({"UnusedDeclaration"})
3780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghuipublic class AnimatedVectorDrawableDupPerf extends Activity {
3880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
3980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    private static final String LOGTAG = "AnimatedVectorDrawableDupPerf";
4080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    protected int[] icon = {
4180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
4980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
5980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
6080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            R.drawable.animation_vector_linear_progress_bar,
6180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui   };
6280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
6380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    /** @hide */
6480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    public static AnimatedVectorDrawable create(Resources resources, int rid) {
6580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        try {
6680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            final XmlPullParser parser = resources.getXml(rid);
6780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            final AttributeSet attrs = Xml.asAttributeSet(parser);
6880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            int type;
6980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            while ((type=parser.next()) != XmlPullParser.START_TAG &&
7080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui                    type != XmlPullParser.END_DOCUMENT) {
7180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui                // Empty loop
7280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            }
7380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            if (type != XmlPullParser.START_TAG) {
7480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui                throw new XmlPullParserException("No start tag found");
7580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            }
7680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
7780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            final AnimatedVectorDrawable drawable = new AnimatedVectorDrawable();
7880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            drawable.inflate(resources, parser, attrs);
7980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
8080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            return drawable;
8180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        } catch (XmlPullParserException e) {
8280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            Log.e(LOGTAG, "parser error", e);
8380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        } catch (IOException e) {
8480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            Log.e(LOGTAG, "parser error", e);
8580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        }
8680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        return null;
8780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    }
8880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
8980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    @Override
9080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    protected void onCreate(Bundle savedInstanceState) {
9180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        super.onCreate(savedInstanceState);
9280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        ScrollView scrollView = new ScrollView(this);
9380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        GridLayout container = new GridLayout(this);
9480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        scrollView.addView(container);
9580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        container.setColumnCount(5);
9680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        Resources res = this.getResources();
9780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        container.setBackgroundColor(0xFF888888);
9880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        AnimatedVectorDrawable []d = new AnimatedVectorDrawable[icon.length];
9980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        long time =  android.os.SystemClock.elapsedRealtimeNanos();
10080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        for (int i = 0; i < icon.length; i++) {
10180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui             d[i] = create(res,icon[i]);
10280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        }
10380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        time =  android.os.SystemClock.elapsedRealtimeNanos()-time;
10480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        TextView t = new TextView(this);
10580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        DecimalFormat df = new DecimalFormat("#.##");
10680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        t.setText("avgL=" + df.format(time / (icon.length * 1000000.)) + " ms");
10780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        container.addView(t);
10880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        time =  android.os.SystemClock.elapsedRealtimeNanos();
10980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        for (int i = 0; i < icon.length; i++) {
11080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            Button button = new Button(this);
11180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            button.setWidth(200);
11280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            button.setBackgroundResource(icon[i]);
11380f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui            container.addView(button);
11480f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        }
11580f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        setContentView(scrollView);
11680f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        time =  android.os.SystemClock.elapsedRealtimeNanos()-time;
11780f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        t = new TextView(this);
11880f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        t.setText("avgS=" + df.format(time / (icon.length * 1000000.)) + " ms");
11980f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui        container.addView(t);
12080f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui    }
12180f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui
12280f44408a19485a1ca134ec88c14e8bc975c0ce3ztenghui}
123