1 /*
2  * Copyright (C) 2012 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.hwui;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.widget.LinearLayout;
26 
27 @SuppressWarnings("UnusedDeclaration")
28 public class MultiLayersActivity extends Activity {
29     @Override
onCreate(Bundle savedInstanceState)30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32 
33         LinearLayout grid = new LinearLayout(this);
34         grid.setOrientation(LinearLayout.VERTICAL);
35 
36         LinearLayout row1 = new LinearLayout(this);
37         row1.setOrientation(LinearLayout.HORIZONTAL);
38         grid.addView(row1, new LinearLayout.LayoutParams(
39                 LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.0f));
40 
41         LinearLayout row2 = new LinearLayout(this);
42         row2.setOrientation(LinearLayout.HORIZONTAL);
43         grid.addView(row2, new LinearLayout.LayoutParams(
44                 LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.0f));
45 
46         row1.addView(new LayerView(this, 0xffff0000), new LinearLayout.LayoutParams(
47                 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
48         row1.addView(new LayerView(this, 0x0f00ff00), new LinearLayout.LayoutParams(
49                 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
50 
51         row2.addView(new LayerView(this, 0x0f0000ff), new LinearLayout.LayoutParams(
52                 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
53         row2.addView(new LayerView(this, 0xffffff00), new LinearLayout.LayoutParams(
54                 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
55 
56         setContentView(grid);
57     }
58 
59     private class LayerView extends View {
60         private final Paint mPaint;
61 
LayerView(Context context, int color)62         public LayerView(Context context, int color) {
63             super(context);
64             mPaint = new Paint();
65             mPaint.setColor(color);
66             setLayerType(LAYER_TYPE_HARDWARE, null);
67         }
68 
69         @Override
onDraw(Canvas canvas)70         protected void onDraw(Canvas canvas) {
71             canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint);
72             invalidate();
73         }
74     }
75 }
76