• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Lines2Activity 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 LinesView gpuView = new LinesView(this, 0, Color.GREEN);
40         frame.addView(gpuView);
41         final LinesView swView = new LinesView(this, 400, Color.RED);
42         swView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
43         frame.addView(swView);
44         final LinesView hwBothView = new LinesView(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 LinesView swBothView = new LinesView(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 LinesView extends View {
61 
62         private float mOffset;
63         private int mColor;
64         private float[] basePoints = {
65                 120, 0, 120, 20, 120, 20, 125, 0, 130, 0, 132, 10
66         };
67         private float[] copyPoints = new float[12];
68 
LinesView(Context c, float offset, int color)69         public LinesView(Context c, float offset, int color) {
70             super(c);
71             mOffset = offset;
72             mColor = color;
73         }
74 
drawLines(Canvas canvas, Paint p, float xOffset, float yOffset)75         private void drawLines(Canvas canvas, Paint p, float xOffset, float yOffset) {
76             canvas.drawLine(10 + xOffset, yOffset, 10 + xOffset, 40 + yOffset, p);
77             canvas.drawLine(30 + xOffset, yOffset, 40 + xOffset, 40 + yOffset, p);
78             canvas.drawLine(40 + xOffset, yOffset, 75 + xOffset, 35 + yOffset, p);
79             canvas.drawLine(50 + xOffset, 5+ yOffset, 100 + xOffset, 15 + yOffset, p);
80             canvas.drawLine(60 + xOffset, yOffset, 110 + xOffset, 2 + yOffset, p);
81             canvas.drawLine(60 + xOffset, 40 + yOffset, 110 + xOffset, 40 + yOffset, p);
82             for (int i = 0; i < 12; i += 2) {
83                 copyPoints[i] = basePoints[i] + xOffset;
84                 copyPoints[i+1] = basePoints[i+1] + yOffset;
85             }
86             canvas.drawLines(copyPoints, 0, 12, p);
87         }
88 
drawVerticalLine(Canvas canvas, Paint p, float length, float x, float y)89         private void drawVerticalLine(Canvas canvas, Paint p, float length, float x, float y) {
90             canvas.drawLine(x, y, x, y + length, p);
91         }
92 
drawDiagonalLine(Canvas canvas, Paint p, float length, float x, float y)93         private void drawDiagonalLine(Canvas canvas, Paint p, float length, float x, float y) {
94             canvas.drawLine(x, y, x + length, y + length, p);
95         }
96 
97         @Override
onDraw(Canvas canvas)98         protected void onDraw(Canvas canvas) {
99             super.onDraw(canvas);
100             Paint p = new Paint();
101             p.setColor(mColor);
102             float yOffset = 10;
103 
104             canvas.save();
105             drawLines(canvas, p, mOffset, yOffset);
106             canvas.scale(2, 2);
107             canvas.translate(60, 0);
108             drawLines(canvas, p, mOffset/2, yOffset/2);
109             canvas.restore();
110 
111             yOffset +=100;
112             canvas.save();
113             p.setStrokeWidth(1);
114             drawLines(canvas, p, mOffset, yOffset);
115             canvas.scale(2, 2);
116             canvas.translate(60, 0);
117             drawLines(canvas, p, mOffset/2, yOffset/2);
118             canvas.restore();
119 
120             yOffset += 100;
121             canvas.save();
122             p.setStrokeWidth(2);
123             drawLines(canvas, p, mOffset, yOffset);
124             canvas.scale(2, 2);
125             canvas.translate(60, 0);
126             drawLines(canvas, p, mOffset/2, yOffset/2);
127             canvas.restore();
128 
129             p.setAntiAlias(true);
130             p.setStrokeWidth(0);
131             yOffset += 100;
132             canvas.save();
133             drawLines(canvas, p, mOffset, yOffset);
134             canvas.scale(2, 2);
135             canvas.translate(60, 0);
136             drawLines(canvas, p, mOffset/2, yOffset/2);
137             canvas.restore();
138 
139             yOffset += 100;
140             canvas.save();
141             p.setStrokeWidth(1);
142             drawLines(canvas, p, mOffset, yOffset);
143             canvas.scale(2, 2);
144             canvas.translate(60, 0);
145             drawLines(canvas, p, mOffset/2, yOffset/2);
146             canvas.restore();
147 
148             yOffset += 100;
149             canvas.save();
150             p.setStrokeWidth(2);
151             drawLines(canvas, p, mOffset, yOffset);
152             canvas.scale(2, 2);
153             canvas.translate(60, 0);
154             drawLines(canvas, p, mOffset/2, yOffset/2);
155             canvas.restore();
156 
157             yOffset += 100;
158             canvas.save();
159             p.setStrokeWidth(1);
160             float x = 10 + mOffset;
161             for (float length = 1; length <= 10; length +=1 ) {
162                 p.setAntiAlias(false);
163                 drawVerticalLine(canvas, p, length, x, yOffset);
164                 x += 5;
165                 p.setAntiAlias(true);
166                 drawVerticalLine(canvas, p, length, x, yOffset);
167                 x += 5;
168             }
169             p.setStrokeWidth(5);
170             for (float length = 1; length <= 10; length +=1 ) {
171                 p.setAntiAlias(false);
172                 drawVerticalLine(canvas, p, length, x, yOffset);
173                 x += 10;
174                 p.setAntiAlias(true);
175                 drawVerticalLine(canvas, p, length, x, yOffset);
176                 x += 10;
177             }
178             canvas.restore();
179 
180             yOffset += 20;
181             canvas.save();
182             p.setStrokeWidth(1);
183             x = 10 + mOffset;
184             for (float length = 1; length <= 10; length +=1 ) {
185                 p.setAntiAlias(false);
186                 drawDiagonalLine(canvas, p, length, x, yOffset);
187                 x += 5;
188                 p.setAntiAlias(true);
189                 drawDiagonalLine(canvas, p, length, x, yOffset);
190                 x += 5;
191             }
192             p.setStrokeWidth(2);
193             for (float length = 1; length <= 10; length +=1 ) {
194                 p.setAntiAlias(false);
195                 drawDiagonalLine(canvas, p, length, x, yOffset);
196                 x += 10;
197                 p.setAntiAlias(true);
198                 drawDiagonalLine(canvas, p, length, x, yOffset);
199                 x += 10;
200             }
201             canvas.restore();
202 
203             yOffset += 20;
204             canvas.save();
205             p.setStrokeWidth(1);
206             x = 10 + mOffset;
207             for (float length = 1; length <= 10; length +=1 ) {
208                 p.setAntiAlias(false);
209                 canvas.drawLine(x, yOffset, x + 1, yOffset + length, p);
210                 x += 5;
211                 p.setAntiAlias(true);
212                 canvas.drawLine(x, yOffset, x + 1, yOffset + length, p);
213                 x += 5;
214             }
215             p.setStrokeWidth(2);
216             for (float length = 1; length <= 10; length +=1 ) {
217                 p.setAntiAlias(false);
218                 canvas.drawLine(x, yOffset, x + 1, yOffset + length, p);
219                 x += 10;
220                 p.setAntiAlias(true);
221                 canvas.drawLine(x, yOffset, x + 1, yOffset + length, p);
222                 x += 10;
223             }
224             canvas.restore();
225 
226             yOffset += 20;
227             canvas.save();
228             p.setStrokeWidth(1);
229             x = 10 + mOffset;
230             for (float length = 1; length <= 10; length +=1 ) {
231                 p.setAntiAlias(false);
232                 canvas.drawLine(x, yOffset, x + length, yOffset + 1, p);
233                 x += 5;
234                 p.setAntiAlias(true);
235                 canvas.drawLine(x, yOffset, x + length, yOffset + 1, p);
236                 x += 5;
237             }
238             p.setStrokeWidth(2);
239             for (float length = 1; length <= 10; length +=1 ) {
240                 p.setAntiAlias(false);
241                 canvas.drawLine(x, yOffset, x + length, yOffset + 1, p);
242                 x += 10;
243                 p.setAntiAlias(true);
244                 canvas.drawLine(x, yOffset, x + length, yOffset + 1, p);
245                 x += 10;
246             }
247             canvas.restore();
248 
249         }
250     }
251 }
252