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