blob: b1c4486a7d33141db26e164cb5d5982c6c755f66 [file] [log] [blame]
Philip Milneaa616f32011-05-27 18:38:01 -07001/*
2 * Copyright (C) 2011 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.android.test.layout;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.view.View;
23import android.view.ViewGroup;
Philip Milneaa616f32011-05-27 18:38:01 -070024import android.widget.Button;
25import android.widget.EditText;
26import android.widget.GridLayout;
27import android.widget.TextView;
28
29import static android.widget.GridLayout.*;
30
31public class AlignmentTest extends Activity {
32
Philip Milne7fd94872011-06-07 20:14:17 -070033 public static final String[] HORIZONTAL_NAMES = {"LEFT", "center", "east", "fill"};
34 public static final Alignment[] HORIZONTAL_ALIGNMENTS = {LEFT, CENTER, RIGHT, FILL};
35 public static final String[] VERTICAL_NAMES = {"north", "center", "baseline", "south", "fill"};
36 public static final Alignment[] VERTICAL_ALIGNMENTS = {TOP, CENTER, BASELINE, BOTTOM, FILL};
Philip Milneaa616f32011-05-27 18:38:01 -070037 private static Context CONTEXT;
38
39 public static interface ViewFactory {
40 View create(String name, int size);
41 }
42
43 public static final ViewFactory BUTTON_FACTORY = new ViewFactory() {
44 public View create(String name, int size) {
45 Button result = new Button(CONTEXT);
46 result.setText(name);
47 result.setOnClickListener(new OnClickListener() {
48 @Override
49 public void onClick(View v) {
50 animate(v);
51 }
52 });
53 return result;
54 }
55 };
56
57 public static final ViewFactory LABEL_FACTORY = new ViewFactory() {
58 public View create(String name, int size) {
59 TextView result = new TextView(CONTEXT);
60 result.setText(name);
61 result.setTextSize(40);
62 return result;
63 }
64 };
65
66 public static final ViewFactory TEXT_FIELD_FACTORY = new ViewFactory() {
67 public View create(String name, int size) {
68 EditText result = new EditText(CONTEXT);
69 result.setText(name);
70 return result;
71 }
72 };
73
Philip Milne7fd94872011-06-07 20:14:17 -070074 public static final ViewFactory[] FACTORIES =
75 {BUTTON_FACTORY, LABEL_FACTORY, TEXT_FIELD_FACTORY};
Philip Milneaa616f32011-05-27 18:38:01 -070076
77 public static ViewGroup create(Context context1) {
78 CONTEXT = context1;
79 GridLayout container = new GridLayout(context1);
80 container.setUseDefaultMargins(true);
81
82 for (int i = 0; i < VERTICAL_ALIGNMENTS.length; i++) {
83 Alignment va = VERTICAL_ALIGNMENTS[i];
84 for (int j = 0; j < HORIZONTAL_ALIGNMENTS.length; j++) {
85 Alignment ha = HORIZONTAL_ALIGNMENTS[j];
Philip Milne93cd6a62011-07-12 14:49:45 -070086 LayoutParams layoutParams = new LayoutParams(spec(i, va), spec(j, ha));
Philip Milne7fd94872011-06-07 20:14:17 -070087 String name = VERTICAL_NAMES[i] + "-" + HORIZONTAL_NAMES[j];
88 ViewFactory factory = FACTORIES[(i + j) % FACTORIES.length];
89 container.addView(factory.create(name, 20), layoutParams);
Philip Milneaa616f32011-05-27 18:38:01 -070090 }
91 }
92
93 return container;
94 }
95
96 public static void animate(View v) {
97
98 long start = System.currentTimeMillis();
99 int N = 1000;
100 for (int i = 0; i < N; i++) {
101 ViewGroup.LayoutParams lp = v.getLayoutParams();
102 lp.width += 1; // width;
103 lp.height += 1; // height;
104 v.requestLayout();
105 GridLayout p = (GridLayout) v.getParent();
106 p.layout(0, 0, 1000 + (i % 2), 500 + (i % 2));
107 }
Philip Milne7fd94872011-06-07 20:14:17 -0700108 float time = (float) (System.currentTimeMillis() - start) / N * 1000;
109 System.out.println("Time: " + time + "mics");
Philip Milneaa616f32011-05-27 18:38:01 -0700110 }
111
112 protected void onCreate(Bundle savedInstanceState) {
113 super.onCreate(savedInstanceState);
114 setContentView(create(getBaseContext()));
115 }
116
117}