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 17package com.example.android.apis.graphics; 18 19import android.content.Context; 20import android.graphics.*; 21import android.os.Bundle; 22import android.view.View; 23 24public class Regions extends GraphicsActivity { 25 26 @Override 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 final Paint mPaint = new Paint(); 34 private final Rect mRect1 = new Rect(); 35 private final Rect mRect2 = new Rect(); 36 37 public SampleView(Context context) { 38 super(context); 39 setFocusable(true); 40 41 mPaint.setAntiAlias(true); 42 mPaint.setTextSize(16); 43 mPaint.setTextAlign(Paint.Align.CENTER); 44 45 mRect1.set(10, 10, 100, 80); 46 mRect2.set(50, 50, 130, 110); 47 } 48 49 private void drawOriginalRects(Canvas canvas, int alpha) { 50 mPaint.setStyle(Paint.Style.STROKE); 51 mPaint.setColor(Color.RED); 52 mPaint.setAlpha(alpha); 53 drawCentered(canvas, mRect1, mPaint); 54 mPaint.setColor(Color.BLUE); 55 mPaint.setAlpha(alpha); 56 drawCentered(canvas, mRect2, mPaint); 57 58 // restore style 59 mPaint.setStyle(Paint.Style.FILL); 60 } 61 62 private void drawRgn(Canvas canvas, int color, String str, Region.Op op) { 63 if (str != null) { 64 mPaint.setColor(Color.BLACK); 65 canvas.drawText(str, 80, 24, mPaint); 66 } 67 68 Region rgn = new Region(); 69 rgn.set(mRect1); 70 rgn.op(mRect2, op); 71 72 mPaint.setColor(color); 73 RegionIterator iter = new RegionIterator(rgn); 74 Rect r = new Rect(); 75 76 canvas.translate(0, 30); 77 mPaint.setColor(color); 78 while (iter.next(r)) { 79 canvas.drawRect(r, mPaint); 80 } 81 drawOriginalRects(canvas, 0x80); 82 } 83 84 private static void drawCentered(Canvas c, Rect r, Paint p) { 85 float inset = p.getStrokeWidth() * 0.5f; 86 if (inset == 0) { // catch hairlines 87 inset = 0.5f; 88 } 89 c.drawRect(r.left + inset, r.top + inset, 90 r.right - inset, r.bottom - inset, p); 91 } 92 93 @Override 94 protected void onDraw(Canvas canvas) { 95 canvas.drawColor(Color.GRAY); 96 97 canvas.save(); 98 canvas.translate(80, 5); 99 drawOriginalRects(canvas, 0xFF); 100 canvas.restore(); 101 102 mPaint.setStyle(Paint.Style.FILL); 103 104 canvas.save(); 105 canvas.translate(0, 140); 106 drawRgn(canvas, Color.RED, "Union", Region.Op.UNION); 107 canvas.restore(); 108 109 canvas.save(); 110 canvas.translate(0, 280); 111 drawRgn(canvas, Color.BLUE, "Xor", Region.Op.XOR); 112 canvas.restore(); 113 114 canvas.save(); 115 canvas.translate(160, 140); 116 drawRgn(canvas, Color.GREEN, "Difference", Region.Op.DIFFERENCE); 117 canvas.restore(); 118 119 canvas.save(); 120 canvas.translate(160, 280); 121 drawRgn(canvas, Color.WHITE, "Intersect", Region.Op.INTERSECT); 122 canvas.restore(); 123 } 124 } 125} 126 127