1 /*
2  * Copyright (C) 2010 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.graphics.Path;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.View;
27 
28 import java.util.ArrayList;
29 import java.util.Random;
30 
31 @SuppressWarnings({"UnusedDeclaration"})
32 public class PathsCacheActivity extends Activity {
33     private Path mPath;
34 
35     private final Random mRandom = new Random();
36     @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
37     private final ArrayList<Path> mPathList = new ArrayList<Path>();
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42 
43         mPath = makePath();
44 
45         final PathsView view = new PathsView(this);
46         setContentView(view);
47     }
48 
makePath()49     private static Path makePath() {
50         Path path = new Path();
51         buildPath(path);
52         return path;
53     }
54 
buildPath(Path path)55     private static void buildPath(Path path) {
56         path.moveTo(0.0f, 0.0f);
57         path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
58         path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
59         path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
60     }
61 
makeLargePath()62     private static Path makeLargePath() {
63         Path path = new Path();
64         buildLargePath(path);
65         return path;
66     }
67 
buildLargePath(Path path)68     private static void buildLargePath(Path path) {
69         path.moveTo(0.0f, 0.0f);
70         path.cubicTo(0.0f, 0.0f, 10000.0f, 15000.0f, 10000.0f, 20000.0f);
71         path.cubicTo(10000.0f, 20000.0f, 5000.0f, 30000.0f, -8000.0f, 20000.0f);
72         path.cubicTo(-8000.0f, 20000.0f, 10000.0f, 20000.0f, 20000.0f, 0.0f);
73     }
74 
75     public class PathsView extends View {
76         private final Paint mMediumPaint;
77 
PathsView(Context c)78         public PathsView(Context c) {
79             super(c);
80 
81             mMediumPaint = new Paint();
82             mMediumPaint.setAntiAlias(true);
83             mMediumPaint.setColor(0xe00000ff);
84             mMediumPaint.setStrokeWidth(10.0f);
85             mMediumPaint.setStyle(Paint.Style.STROKE);
86         }
87 
88         @Override
onDraw(Canvas canvas)89         protected void onDraw(Canvas canvas) {
90             super.onDraw(canvas);
91 
92             canvas.drawARGB(255, 255, 255, 255);
93 
94             canvas.save();
95             canvas.translate(550.0f, 60.0f);
96             canvas.drawPath(mPath, mMediumPaint);
97 
98             mPath.reset();
99             buildPath(mPath);
100 
101             canvas.translate(30.0f, 30.0f);
102             canvas.drawPath(mPath, mMediumPaint);
103             canvas.drawPath(mPath, mMediumPaint);
104 
105             mPath.reset();
106             buildPath(mPath);
107 
108             canvas.translate(30.0f, 30.0f);
109             canvas.drawPath(mPath, mMediumPaint);
110             canvas.drawPath(mPath, mMediumPaint);
111 
112             canvas.restore();
113 
114             for (int i = 0; i < mRandom.nextInt(20); i++) {
115                 Path path = makePath();
116                 int r = mRandom.nextInt(10);
117                 if (r == 5 || r == 3) {
118                     mPathList.add(path);
119                 } else if (r == 7) {
120                     path = makeLargePath();
121                     mPathList.add(path);
122                 }
123 
124                 canvas.save();
125                 canvas.translate(450.0f + mRandom.nextInt(200), mRandom.nextInt(200));
126                 canvas.drawPath(path, mMediumPaint);
127                 canvas.restore();
128             }
129 
130             int r = mRandom.nextInt(100);
131             if (r == 50) {
132                 mPathList.clear();
133             }
134 
135             invalidate();
136         }
137     }
138 }
139