AlignmentTest.java revision 93cd6a6c78683643de51f9e698b38847bd1f1155
1aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne/*
2aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Copyright (C) 2011 The Android Open Source Project
3aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
4aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Licensed under the Apache License, Version 2.0 (the "License");
5aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * you may not use this file except in compliance with the License.
6aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * You may obtain a copy of the License at
7aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
8aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *      http://www.apache.org/licenses/LICENSE-2.0
9aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
10aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Unless required by applicable law or agreed to in writing, software
11aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * distributed under the License is distributed on an "AS IS" BASIS,
12aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * See the License for the specific language governing permissions and
14aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * limitations under the License.
15aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */
16aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
17aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milnepackage com.android.test.layout;
18aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
19aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.app.Activity;
20aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.content.Context;
21aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.os.Bundle;
22aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.view.View;
23aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.view.ViewGroup;
24aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.Button;
25aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.EditText;
26aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.GridLayout;
27aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.TextView;
28aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
29aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport static android.widget.GridLayout.*;
30aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
31aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milnepublic class AlignmentTest  extends Activity {
32aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
337fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final String[] HORIZONTAL_NAMES = {"LEFT", "center", "east", "fill"};
347fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final Alignment[] HORIZONTAL_ALIGNMENTS = {LEFT, CENTER, RIGHT, FILL};
357fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final String[] VERTICAL_NAMES = {"north", "center", "baseline", "south", "fill"};
367fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final Alignment[] VERTICAL_ALIGNMENTS = {TOP, CENTER, BASELINE, BOTTOM, FILL};
37aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    private static Context CONTEXT;
38aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
39aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static interface ViewFactory {
40aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        View create(String name, int size);
41aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
42aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
43aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static final ViewFactory BUTTON_FACTORY = new ViewFactory() {
44aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        public View create(String name, int size) {
45aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            Button result = new Button(CONTEXT);
46aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setText(name);
47aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne           result.setOnClickListener(new OnClickListener() {
48aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne               @Override
49aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne               public void onClick(View v) {
50aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne                    animate(v);
51aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne               }
52aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne           });
53aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            return result;
54aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
55aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    };
56aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
57aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static final ViewFactory LABEL_FACTORY = new ViewFactory() {
58aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        public View create(String name, int size) {
59aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            TextView result = new TextView(CONTEXT);
60aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setText(name);
61aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setTextSize(40);
62aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            return result;
63aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
64aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    };
65aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
66aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static final ViewFactory TEXT_FIELD_FACTORY = new ViewFactory() {
67aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        public View create(String name, int size) {
68aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            EditText result = new EditText(CONTEXT);
69aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            result.setText(name);
70aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            return result;
71aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
72aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    };
73aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
747fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne    public static final ViewFactory[] FACTORIES =
757fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                            {BUTTON_FACTORY, LABEL_FACTORY, TEXT_FIELD_FACTORY};
76aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
77aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static ViewGroup create(Context context1) {
78aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        CONTEXT = context1;
79aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        GridLayout container = new GridLayout(context1);
80aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        container.setUseDefaultMargins(true);
81aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
82aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        for (int i = 0; i < VERTICAL_ALIGNMENTS.length; i++) {
83aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            Alignment va = VERTICAL_ALIGNMENTS[i];
84aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            for (int j = 0; j < HORIZONTAL_ALIGNMENTS.length; j++) {
85aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne                Alignment ha = HORIZONTAL_ALIGNMENTS[j];
8693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne                LayoutParams layoutParams = new LayoutParams(spec(i, va), spec(j, ha));
877fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                String name = VERTICAL_NAMES[i] + "-" + HORIZONTAL_NAMES[j];
887fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                ViewFactory factory = FACTORIES[(i + j) % FACTORIES.length];
897fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne                container.addView(factory.create(name, 20), layoutParams);
90aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            }
91aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
92aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
93aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        return container;
94aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
95aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
96aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public static void animate(View v) {
97aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
98aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        long start = System.currentTimeMillis();
99aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        int N = 1000;
100aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        for (int i = 0; i < N; i++) {
101aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            ViewGroup.LayoutParams lp = v.getLayoutParams();
102aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            lp.width += 1; // width;
103aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            lp.height += 1; // height;
104aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            v.requestLayout();
105aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            GridLayout p = (GridLayout) v.getParent();
106aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            p.layout(0, 0, 1000 + (i % 2), 500 + (i % 2));
107aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
1087fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne        float time = (float) (System.currentTimeMillis() - start) / N * 1000;
1097fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne        System.out.println("Time: " + time + "mics");
110aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
111aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
112aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    protected void onCreate(Bundle savedInstanceState) {
113aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        super.onCreate(savedInstanceState);
114aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        setContentView(create(getBaseContext()));
115aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
116aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
117aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne}