Home | History | Annotate | Download | only in graphics
      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.app.Activity;
     20 import android.content.Context;
     21 import android.graphics.*;
     22 import android.os.Bundle;
     23 import android.view.View;
     24 
     25 public class Regions extends GraphicsActivity {
     26 
     27     @Override
     28     protected void onCreate(Bundle savedInstanceState) {
     29         super.onCreate(savedInstanceState);
     30         setContentView(new SampleView(this));
     31     }
     32 
     33     private static class SampleView extends View {
     34         private final Paint mPaint = new Paint();
     35         private final Rect  mRect1 = new Rect();
     36         private final Rect  mRect2 = new Rect();
     37 
     38         public SampleView(Context context) {
     39             super(context);
     40             setFocusable(true);
     41 
     42             mPaint.setAntiAlias(true);
     43             mPaint.setTextSize(16);
     44             mPaint.setTextAlign(Paint.Align.CENTER);
     45 
     46             mRect1.set(10, 10, 100, 80);
     47             mRect2.set(50, 50, 130, 110);
     48         }
     49 
     50         private void drawOriginalRects(Canvas canvas, int alpha) {
     51             mPaint.setStyle(Paint.Style.STROKE);
     52             mPaint.setColor(Color.RED);
     53             mPaint.setAlpha(alpha);
     54             drawCentered(canvas, mRect1, mPaint);
     55             mPaint.setColor(Color.BLUE);
     56             mPaint.setAlpha(alpha);
     57             drawCentered(canvas, mRect2, mPaint);
     58 
     59             // restore style
     60             mPaint.setStyle(Paint.Style.FILL);
     61         }
     62 
     63         private void drawRgn(Canvas canvas, int color, String str, Region.Op op) {
     64             if (str != null) {
     65                 mPaint.setColor(Color.BLACK);
     66                 canvas.drawText(str, 80, 24, mPaint);
     67             }
     68 
     69             Region rgn = new Region();
     70             rgn.set(mRect1);
     71             rgn.op(mRect2, op);
     72 
     73             mPaint.setColor(color);
     74             RegionIterator iter = new RegionIterator(rgn);
     75             Rect r = new Rect();
     76 
     77             canvas.translate(0, 30);
     78             mPaint.setColor(color);
     79             while (iter.next(r)) {
     80                 canvas.drawRect(r, mPaint);
     81             }
     82             drawOriginalRects(canvas, 0x80);
     83         }
     84 
     85         private static void drawCentered(Canvas c, Rect r, Paint p) {
     86             float inset = p.getStrokeWidth() * 0.5f;
     87             if (inset == 0) {   // catch hairlines
     88                 inset = 0.5f;
     89             }
     90             c.drawRect(r.left + inset, r.top + inset,
     91                        r.right - inset, r.bottom - inset, p);
     92         }
     93 
     94         @Override 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