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.util.Log;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.Button;
26
27import static android.view.Gravity.BOTTOM;
28import static android.view.Gravity.CENTER;
29import static android.view.Gravity.FILL;
30import static android.view.Gravity.LEFT;
31import static android.view.Gravity.NO_GRAVITY;
32import static android.view.Gravity.RIGHT;
33import static android.view.Gravity.TOP;
34
35public 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
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
53    public abstract ViewGroup create(Context context);
54    public abstract String tag();
55
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
72    protected void onCreate(Bundle savedInstanceState) {
73        super.onCreate(savedInstanceState);
74        setContentView(create(getBaseContext()));
75    }
76
77}