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
18package com.android.test.hwui;
19
20import android.app.Activity;
21import android.content.Context;
22import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.os.Bundle;
25import android.util.Log;
26import android.view.Gravity;
27import android.view.View;
28import android.widget.Button;
29import android.widget.LinearLayout;
30
31import static android.view.View.LAYER_TYPE_HARDWARE;
32import static android.view.View.LAYER_TYPE_SOFTWARE;
33import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
34
35@SuppressWarnings({"UnusedDeclaration"})
36public class DisplayListLayersActivity extends Activity {
37    private static final int VERTICAL_MARGIN = 12;
38
39    @Override
40    protected void onCreate(Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42
43        LinearLayout root = createContainer();
44        addChild(root, new LayerView(this, 0xffff0000, LAYER_TYPE_HARDWARE, "hardware"),
45                WRAP_CONTENT, WRAP_CONTENT);
46        addChild(root, new LayerView(this, 0xff0000ff, LAYER_TYPE_SOFTWARE, "software"),
47                WRAP_CONTENT, WRAP_CONTENT);
48        addChild(root, createButton(root), WRAP_CONTENT, WRAP_CONTENT);
49
50        setContentView(root);
51    }
52
53    private Button createButton(final LinearLayout root) {
54        Button button = new Button(this);
55        button.setText("Invalidate");
56        button.setOnClickListener(new View.OnClickListener() {
57            @Override
58            public void onClick(View v) {
59                for (int i = 0; i < root.getChildCount(); i++) {
60                    View child = root.getChildAt(i);
61                    if (child != v) {
62                        child.invalidate();
63                    }
64                }
65            }
66        });
67
68        return button;
69    }
70
71    private void addChild(LinearLayout root, View child, int width, int height) {
72        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
73        params.gravity = Gravity.CENTER_HORIZONTAL;
74        params.setMargins(0, dipToPx(VERTICAL_MARGIN), 0, 0);
75        root.addView(child, params);
76    }
77
78    private int dipToPx(int size) {
79        return (int) (getResources().getDisplayMetrics().density * size + 0.5f);
80    }
81
82    private LinearLayout createContainer() {
83        LinearLayout layout = new LinearLayout(this);
84        layout.setOrientation(LinearLayout.VERTICAL);
85        return layout;
86    }
87
88    private class LayerView extends View {
89        private static final String LOG_TAG = "LayerView";
90        private final Paint mPaint = new Paint();
91
92        private final String mTag;
93
94        LayerView(Context context, int color, int layerType, String tag) {
95            super(context);
96
97            mTag = tag;
98
99            mPaint.setColor(color);
100            setLayerType(layerType, null);
101        }
102
103        private void log(String tag) {
104            Log.d(LOG_TAG, mTag + ": " + tag);
105        }
106
107        @Override
108        public void invalidate() {
109            log("invalidate");
110            super.invalidate();
111        }
112
113        @Override
114        protected void onDraw(Canvas canvas) {
115            log("draw");
116            canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
117        }
118
119        @Override
120        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
121            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec) / 3,
122                    MeasureSpec.getSize(heightMeasureSpec) / 3);
123        }
124    }
125}
126