• 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.ViewGroup;
24 import android.widget.Button;
25 import android.widget.EditText;
26 import android.widget.GridLayout;
27 import android.widget.TextView;
28 
29 import static android.widget.GridLayout.*;
30 
31 public class AlignmentTest  extends Activity {
32 
33     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};
37     private static Context CONTEXT;
38 
39     public static interface ViewFactory {
create(String name, int size)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 
74     public static final ViewFactory[] FACTORIES =
75                             {BUTTON_FACTORY, LABEL_FACTORY, TEXT_FIELD_FACTORY};
76 
create(Context context1)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];
86                 LayoutParams layoutParams = new LayoutParams(spec(i, va), spec(j, ha));
87                 String name = VERTICAL_NAMES[i] + "-" + HORIZONTAL_NAMES[j];
88                 ViewFactory factory = FACTORIES[(i + j) % FACTORIES.length];
89                 container.addView(factory.create(name, 20), layoutParams);
90             }
91         }
92 
93         return container;
94     }
95 
animate(View v)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         }
108         float time = (float) (System.currentTimeMillis() - start) / N * 1000;
109         System.out.println("Time: " + time + "mics");
110     }
111 
onCreate(Bundle savedInstanceState)112     protected void onCreate(Bundle savedInstanceState) {
113         super.onCreate(savedInstanceState);
114         setContentView(create(getBaseContext()));
115     }
116 
117 }