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.hwui;
18 
19 import android.animation.ObjectAnimator;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.Paint;
25 import android.graphics.drawable.ColorDrawable;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.widget.FrameLayout;
29 
30 @SuppressWarnings({"UnusedDeclaration"})
31 public class ColoredRectsActivity extends Activity {
32     private ObjectAnimator mAnimator;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         getWindow().setBackgroundDrawable(new ColorDrawable(0xff000000));
38         FrameLayout frame = new FrameLayout(this);
39         final RectsView gpuView = new RectsView(this, 0, Color.GREEN);
40         frame.addView(gpuView);
41         final RectsView swView = new RectsView(this, 400, Color.RED);
42         swView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
43         frame.addView(swView);
44         final RectsView hwBothView = new RectsView(this, 850, Color.GREEN);
45         // Don't actually need to render to a hw layer, but it's a good sanity-check that
46         // we're rendering to/from layers correctly
47         hwBothView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
48         frame.addView(hwBothView);
49         final RectsView swBothView = new RectsView(this, 854, Color.RED);
50         swBothView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
51         frame.addView(swBothView);
52         setContentView(frame);
53     }
54 
55     @Override
onDestroy()56     protected void onDestroy() {
57         super.onDestroy();
58     }
59 
60     public static class RectsView extends View {
61 
62         private float mOffset;
63         private int mColor;
64 
RectsView(Context c, float offset, int color)65         public RectsView(Context c, float offset, int color) {
66             super(c);
67             mOffset = offset;
68             mColor = color;
69         }
70 
71         @Override
onDraw(Canvas canvas)72         protected void onDraw(Canvas canvas) {
73             super.onDraw(canvas);
74             Paint p = new Paint();
75             p.setColor(mColor);
76             float yOffset = 10;
77 
78             for (int i = 0; i < 2; ++i) {
79                 canvas.save();
80                 canvas.translate(mOffset, yOffset);
81                 canvas.drawRect(0, 0, 20, 10, p);
82                 canvas.drawRect(35, 0, 45, 20, p);
83                 canvas.translate(0, -yOffset);
84                 canvas.scale(2, 2);
85                 canvas.translate(60, yOffset/2);
86                 canvas.drawRect(0, 0, 20, 10, p);
87                 canvas.translate(15, 0);
88                 canvas.drawRect(35, 0, 45, 20, p);
89                 canvas.restore();
90 
91                 yOffset += 100;
92 
93                 canvas.save();
94                 canvas.save();
95                 canvas.translate(mOffset + 10, yOffset);
96                 canvas.rotate(45);
97                 canvas.drawRect(0, 0, 20, 10, p);
98                 canvas.restore();
99                 canvas.save();
100                 canvas.translate(mOffset + 70, yOffset);
101                 canvas.rotate(5);
102                 canvas.drawRect(0, 0, 20, 10, p);
103                 canvas.restore();
104                 canvas.save();
105                 canvas.translate(mOffset + 140, yOffset);
106                 canvas.scale(2, 2);
107                 canvas.rotate(5);
108                 canvas.drawRect(0, 0, 20, 10, p);
109                 canvas.restore();
110                 canvas.save();
111                 canvas.translate(mOffset + 210, yOffset);
112                 canvas.scale(2, 2);
113                 canvas.rotate(45);
114                 canvas.drawRect(0, 0, 20, 10, p);
115                 canvas.restore();
116                 canvas.save();
117                 canvas.translate(mOffset + 280, yOffset);
118                 canvas.scale(0.5f, 8);
119                 canvas.rotate(0.5f);
120                 canvas.drawRect(0, 0, 80, 5, p);
121                 canvas.restore();
122                 canvas.restore();
123 
124                 yOffset += 100;
125 
126                 p.setAntiAlias(true);
127             }
128         }
129     }
130 }
131