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 import android.content.Context;
20 import android.graphics.*;
21 import android.os.Bundle;
22 import android.view.View;
23 
24 public class Clipping extends GraphicsActivity {
25 
26     @Override
onCreate(Bundle savedInstanceState)27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(new SampleView(this));
30     }
31 
32     private static class SampleView extends View {
33         private Paint mPaint;
34         private Path mPath;
35 
SampleView(Context context)36         public SampleView(Context context) {
37             super(context);
38             setFocusable(true);
39 
40             mPaint = new Paint();
41             mPaint.setAntiAlias(true);
42             mPaint.setStrokeWidth(6);
43             mPaint.setTextSize(16);
44             mPaint.setTextAlign(Paint.Align.RIGHT);
45 
46             mPath = new Path();
47         }
48 
drawScene(Canvas canvas)49         private void drawScene(Canvas canvas) {
50             canvas.clipRect(0, 0, 100, 100);
51 
52             canvas.drawColor(Color.WHITE);
53 
54             mPaint.setColor(Color.RED);
55             canvas.drawLine(0, 0, 100, 100, mPaint);
56 
57             mPaint.setColor(Color.GREEN);
58             canvas.drawCircle(30, 70, 30, mPaint);
59 
60             mPaint.setColor(Color.BLUE);
61             canvas.drawText("Clipping", 100, 30, mPaint);
62         }
63 
onDraw(Canvas canvas)64         @Override protected void onDraw(Canvas canvas) {
65             canvas.drawColor(Color.GRAY);
66 
67             canvas.save();
68             canvas.translate(10, 10);
69             drawScene(canvas);
70             canvas.restore();
71 
72             canvas.save();
73             canvas.translate(160, 10);
74             canvas.clipRect(10, 10, 90, 90);
75             canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE);
76             drawScene(canvas);
77             canvas.restore();
78 
79             canvas.save();
80             canvas.translate(10, 160);
81             mPath.reset();
82             canvas.clipPath(mPath); // makes the clip empty
83             mPath.addCircle(50, 50, 50, Path.Direction.CCW);
84             canvas.clipPath(mPath);
85             drawScene(canvas);
86             canvas.restore();
87         }
88     }
89 }
90 
91