AlignmentTest.java revision aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232
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 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; 24import android.view.ViewParent; 25import android.widget.Button; 26import android.widget.EditText; 27import android.widget.GridLayout; 28import android.widget.TextView; 29 30import static android.widget.GridLayout.*; 31 32public class AlignmentTest extends Activity { 33 34 public static final String[] HORIZONTAL_NAMES = new String[]{"LEFT", "center", "east", "fill"}; 35 public static final Alignment[] HORIZONTAL_ALIGNMENTS = new Alignment[]{LEFT, CENTER, RIGHT, FILL}; 36 public static final String[] VERTICAL_NAMES = new String[]{"north", "center", "baseline", "south", "fill"}; 37 public static final Alignment[] VERTICAL_ALIGNMENTS = new Alignment[]{TOP, CENTER, BASELINE, BOTTOM, FILL}; 38 private static Context CONTEXT; 39 40 public static interface ViewFactory { 41 View create(String name, int size); 42 } 43 44 public static final ViewFactory BUTTON_FACTORY = new ViewFactory() { 45 public View create(String name, int size) { 46 Button result = new Button(CONTEXT); 47 result.setText(name); 48 result.setOnClickListener(new OnClickListener() { 49 @Override 50 public void onClick(View v) { 51 animate(v); 52 } 53 }); 54 return result; 55 } 56 }; 57 58 public static final ViewFactory LABEL_FACTORY = new ViewFactory() { 59 public View create(String name, int size) { 60 TextView result = new TextView(CONTEXT); 61 result.setText(name); 62 result.setTextSize(40); 63 return result; 64 } 65 }; 66 67 public static final ViewFactory TEXT_FIELD_FACTORY = new ViewFactory() { 68 public View create(String name, int size) { 69 EditText result = new EditText(CONTEXT); 70 result.setText(name); 71 return result; 72 } 73 }; 74 75 public static final ViewFactory[] FACTORIES = new ViewFactory[]{BUTTON_FACTORY, LABEL_FACTORY, TEXT_FIELD_FACTORY}; 76 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 Group rowGroup = new Group(i, va); 87 Group colGroup = new Group(j, ha); 88 LayoutParams layoutParams = new LayoutParams(rowGroup, colGroup); 89 container.addView(FACTORIES[(i + j) % FACTORIES.length].create(VERTICAL_NAMES[i] + "-" + HORIZONTAL_NAMES[j], 20), layoutParams); 90 } 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 } 108 System.out.println("Time: " + (float) (System.currentTimeMillis() - start) / N * 1000 + "mics"); 109 } 110 111 protected void onCreate(Bundle savedInstanceState) { 112 super.onCreate(savedInstanceState); 113 setContentView(create(getBaseContext())); 114 } 115 116}