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