1 /*
2  * Copyright (C) 2008 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.example.android.apis.graphics;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 //import com.example.android.apis.R;
22 
23 import android.content.Context;
24 import android.graphics.*;
25 import android.os.Bundle;
26 import android.view.View;
27 
28 public class PathFillTypes extends GraphicsActivity {
29 
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(new SampleView(this));
34     }
35 
36     private static class SampleView extends View {
37         private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
38         private Path mPath;
39 
SampleView(Context context)40         public SampleView(Context context) {
41             super(context);
42             setFocusable(true);
43             setFocusableInTouchMode(true);
44 
45             mPath = new Path();
46             mPath.addCircle(40, 40, 45, Path.Direction.CCW);
47             mPath.addCircle(80, 80, 45, Path.Direction.CCW);
48         }
49 
showPath(Canvas canvas, int x, int y, Path.FillType ft, Paint paint)50         private void showPath(Canvas canvas, int x, int y, Path.FillType ft,
51                               Paint paint) {
52             canvas.save();
53             canvas.translate(x, y);
54             canvas.clipRect(0, 0, 120, 120);
55             canvas.drawColor(Color.WHITE);
56             mPath.setFillType(ft);
57             canvas.drawPath(mPath, paint);
58             canvas.restore();
59         }
60 
onDraw(Canvas canvas)61         @Override protected void onDraw(Canvas canvas) {
62             Paint paint = mPaint;
63 
64             canvas.drawColor(0xFFCCCCCC);
65 
66             canvas.translate(20, 20);
67 
68             paint.setAntiAlias(true);
69 
70             showPath(canvas, 0, 0, Path.FillType.WINDING, paint);
71             showPath(canvas, 160, 0, Path.FillType.EVEN_ODD, paint);
72             showPath(canvas, 0, 160, Path.FillType.INVERSE_WINDING, paint);
73             showPath(canvas, 160, 160, Path.FillType.INVERSE_EVEN_ODD, paint);
74         }
75     }
76 }
77 
78