• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.RectF;
25 import android.os.Bundle;
26 import android.view.View;
27 
28 @SuppressWarnings({"UnusedDeclaration"})
29 public class ScaledPathsActivity extends Activity {
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         final PathsView view = new PathsView(this);
34         setContentView(view);
35     }
36 
37     public static class PathsView extends View {
38         private final Paint mPathPaint;
39         private final Path mPath;
40         private final RectF mPathBounds = new RectF();
41 
PathsView(Context c)42         public PathsView(Context c) {
43             super(c);
44 
45             mPathPaint = new Paint();
46             mPathPaint.setAntiAlias(true);
47             mPathPaint.setColor(0xff0000ff);
48             mPathPaint.setStrokeWidth(5.0f);
49             mPathPaint.setStyle(Paint.Style.FILL);
50 
51             mPath = new Path();
52             mPath.moveTo(0.0f, 0.0f);
53             mPath.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
54             mPath.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
55             mPath.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
56 
57             mPath.computeBounds(mPathBounds, true);
58         }
59 
60         @Override
onDraw(Canvas canvas)61         protected void onDraw(Canvas canvas) {
62             super.onDraw(canvas);
63             canvas.drawARGB(255, 255, 255, 255);
64 
65             mPathPaint.setColor(0xff0000ff);
66             mPathPaint.setStyle(Paint.Style.FILL);
67 
68             canvas.save();
69             drawPath(canvas, 1.0f, 1.0f);
70             drawPath(canvas, 2.0f, 2.0f);
71             drawPath(canvas, 4.0f, 4.0f);
72             canvas.restore();
73 
74             mPathPaint.setColor(0xffff0000);
75             mPathPaint.setStyle(Paint.Style.STROKE);
76 
77             canvas.save();
78             drawPath(canvas, 1.0f, 1.0f);
79             drawPath(canvas, 2.0f, 2.0f);
80             drawPath(canvas, 4.0f, 4.0f);
81             canvas.restore();
82         }
83 
drawPath(Canvas canvas, float scaleX, float scaleY)84         private void drawPath(Canvas canvas, float scaleX, float scaleY) {
85             canvas.save();
86             canvas.scale(scaleX, scaleY);
87             canvas.drawPath(mPath, mPathPaint);
88             canvas.restore();
89             canvas.translate(mPathBounds.width() * scaleX, 0.0f);
90         }
91     }
92 }
93