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.util.Log; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.Button; 26 27 import static android.view.Gravity.BOTTOM; 28 import static android.view.Gravity.CENTER; 29 import static android.view.Gravity.FILL; 30 import static android.view.Gravity.LEFT; 31 import static android.view.Gravity.NO_GRAVITY; 32 import static android.view.Gravity.RIGHT; 33 import static android.view.Gravity.TOP; 34 35 public abstract class AbstractLayoutTest extends Activity { 36 37 public static final String[] HORIZONTAL_NAMES = { "LEFT", "CENTER", "RIGHT", "FILL" }; 38 public static final int[] HORIZONTAL_ALIGNMENTS = { LEFT, CENTER, RIGHT, FILL }; 39 public static final String[] VERTICAL_NAMES = { "TOP", "CENTER", "BASELINE", "BOTTOM", "FILL" }; 40 public static final int[] VERTICAL_ALIGNMENTS = { TOP, CENTER, NO_GRAVITY, BOTTOM, FILL }; 41 create(Context context, String name, int size)42 public View create(Context context, String name, int size) { 43 Button result = new Button(context); 44 result.setText(name); 45 result.setOnClickListener(new View.OnClickListener() { 46 public void onClick(View v) { 47 animate(v); 48 } 49 }); 50 return result; 51 } 52 create(Context context)53 public abstract ViewGroup create(Context context); tag()54 public abstract String tag(); 55 animate(View v)56 public void animate(View v) { 57 long start = System.currentTimeMillis(); 58 int N = 1000; 59 for (int i = 0; i < N; i++) { 60 ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); 61 lp.topMargin = (lp.topMargin + 1) % 31; 62 lp.leftMargin = (lp.leftMargin + 1) % 31; 63 64 v.requestLayout(); 65 v.invalidate(); 66 ViewGroup p = (ViewGroup) v.getParent(); 67 p.layout(0, 0, 1000 + (i % 2), 500 + (i % 2)); 68 } 69 Log.d(tag(), "Time: " + (float) (System.currentTimeMillis() - start) / N * 1000 + "mics"); 70 } 71 onCreate(Bundle savedInstanceState)72 protected void onCreate(Bundle savedInstanceState) { 73 super.onCreate(savedInstanceState); 74 setContentView(create(getBaseContext())); 75 } 76 77 }