• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.test.layout;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.view.ViewGroup;
25 import android.widget.Button;
26 import android.widget.EditText;
27 import android.widget.GridLayout;
28 import android.widget.GridLayout.Alignment;
29 import android.widget.GridLayout.LayoutParams;
30 import android.widget.TextView;
31 
32 import static android.widget.GridLayout.BASELINE;
33 import static android.widget.GridLayout.BOTTOM;
34 import static android.widget.GridLayout.CENTER;
35 import static android.widget.GridLayout.FILL;
36 import static android.widget.GridLayout.LEFT;
37 import static android.widget.GridLayout.RIGHT;
38 import static android.widget.GridLayout.TOP;
39 import static android.widget.GridLayout.spec;
40 
41 public class AlignmentTest extends Activity {
42 
43     public static final String[] HORIZONTAL_NAMES = {"LEFT", "center", "east", "fill"};
44     public static final Alignment[] HORIZONTAL_ALIGNMENTS = {LEFT, CENTER, RIGHT, FILL};
45     public static final String[] VERTICAL_NAMES = {"north", "center", "baseline", "south", "fill"};
46     public static final Alignment[] VERTICAL_ALIGNMENTS = {TOP, CENTER, BASELINE, BOTTOM, FILL};
47     private static Context CONTEXT;
48 
49     public static interface ViewFactory {
create(String name, int size)50         View create(String name, int size);
51     }
52 
53     public static final ViewFactory BUTTON_FACTORY = new ViewFactory() {
54         public View create(String name, int size) {
55             Button result = new Button(CONTEXT);
56             result.setText(name);
57            result.setOnClickListener(new OnClickListener() {
58                @Override
59                public void onClick(View v) {
60                     animate(v);
61                }
62            });
63             return result;
64         }
65     };
66 
67     public static final ViewFactory LABEL_FACTORY = new ViewFactory() {
68         public View create(String name, int size) {
69             TextView result = new TextView(CONTEXT);
70             result.setText(name);
71             result.setTextSize(40);
72             return result;
73         }
74     };
75 
76     public static final ViewFactory TEXT_FIELD_FACTORY = new ViewFactory() {
77         public View create(String name, int size) {
78             EditText result = new EditText(CONTEXT);
79             result.setText(name);
80             return result;
81         }
82     };
83 
84     public static final ViewFactory[] FACTORIES =
85                             {BUTTON_FACTORY, LABEL_FACTORY, TEXT_FIELD_FACTORY};
86 
create(Context context1)87     public static ViewGroup create(Context context1) {
88         CONTEXT = context1;
89         GridLayout container = new GridLayout(context1);
90         container.setUseDefaultMargins(true);
91 
92         for (int i = 0; i < VERTICAL_ALIGNMENTS.length; i++) {
93             Alignment va = VERTICAL_ALIGNMENTS[i];
94             for (int j = 0; j < HORIZONTAL_ALIGNMENTS.length; j++) {
95                 Alignment ha = HORIZONTAL_ALIGNMENTS[j];
96                 LayoutParams layoutParams = new LayoutParams(spec(i, va), spec(j, ha));
97                 String name = VERTICAL_NAMES[i] + "-" + HORIZONTAL_NAMES[j];
98                 ViewFactory factory = FACTORIES[(i + j) % FACTORIES.length];
99                 container.addView(factory.create(name, 20), layoutParams);
100             }
101         }
102 
103         return container;
104     }
105 
animate(View v)106     public static void animate(View v) {
107 
108         long start = System.currentTimeMillis();
109         int N = 1000;
110         for (int i = 0; i < N; i++) {
111             ViewGroup.LayoutParams lp = v.getLayoutParams();
112             lp.width += 1; // width;
113             lp.height += 1; // height;
114             v.requestLayout();
115             GridLayout p = (GridLayout) v.getParent();
116             p.layout(0, 0, 1000 + (i % 2), 500 + (i % 2));
117         }
118         float time = (float) (System.currentTimeMillis() - start) / N * 1000;
119         System.out.println("Time: " + time + "mics");
120     }
121 
onCreate(Bundle savedInstanceState)122     protected void onCreate(Bundle savedInstanceState) {
123         super.onCreate(savedInstanceState);
124         setContentView(create(getBaseContext()));
125     }
126 
127 }