blob: c1e7f4ad156c895467456a75a3f264ac4edc95fc [file] [log] [blame]
Romain Guy96ebc6b2012-02-21 18:32:32 -08001/*
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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.Path;
24import android.os.Bundle;
25import android.util.Log;
26import android.view.View;
27
28import java.util.ArrayList;
29import java.util.Random;
30
31@SuppressWarnings({"UnusedDeclaration"})
32public class PathsCacheActivity extends Activity {
33 private Path mPath;
34
35 private final Random mRandom = new Random();
Romain Guy5dc7fa72013-03-11 20:48:31 -070036 @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
Romain Guy96ebc6b2012-02-21 18:32:32 -080037 private final ArrayList<Path> mPathList = new ArrayList<Path>();
38
39 @Override
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
Romain Guyca89e2a2013-03-08 17:44:20 -080049 private static Path makePath() {
Romain Guy96ebc6b2012-02-21 18:32:32 -080050 Path path = new Path();
51 buildPath(path);
52 return path;
53 }
54
Romain Guyca89e2a2013-03-08 17:44:20 -080055 private static void buildPath(Path path) {
Romain Guy96ebc6b2012-02-21 18:32:32 -080056 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
Romain Guy5dc7fa72013-03-11 20:48:31 -070062 private static Path makeLargePath() {
63 Path path = new Path();
64 buildLargePath(path);
65 return path;
66 }
67
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
Romain Guy96ebc6b2012-02-21 18:32:32 -080075 public class PathsView extends View {
76 private final Paint mMediumPaint;
77
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
89 protected void onDraw(Canvas canvas) {
90 super.onDraw(canvas);
Romain Guy96ebc6b2012-02-21 18:32:32 -080091
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
Romain Guy5d923202013-08-21 18:40:24 -0700105 mPath.reset();
106 buildPath(mPath);
107
108 canvas.translate(30.0f, 30.0f);
109 canvas.drawPath(mPath, mMediumPaint);
110 canvas.drawPath(mPath, mMediumPaint);
111
Romain Guy96ebc6b2012-02-21 18:32:32 -0800112 canvas.restore();
113
Romain Guy4bcb7462012-02-23 17:08:38 -0800114 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);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700119 } else if (r == 7) {
120 path = makeLargePath();
121 mPathList.add(path);
Romain Guy4bcb7462012-02-23 17:08:38 -0800122 }
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
Romain Guy96ebc6b2012-02-21 18:32:32 -0800135 invalidate();
136 }
137 }
138}