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 Clipping 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 Paint mPaint;
     35         private Path mPath;
     36 
     37         public SampleView(Context context) {
     38             super(context);
     39             setFocusable(true);
     40 
     41             mPaint = new Paint();
     42             mPaint.setAntiAlias(true);
     43             mPaint.setStrokeWidth(6);
     44             mPaint.setTextSize(16);
     45             mPaint.setTextAlign(Paint.Align.RIGHT);
     46 
     47             mPath = new Path();
     48         }
     49 
     50         private void drawScene(Canvas canvas) {
     51             canvas.clipRect(0, 0, 100, 100);
     52 
     53             canvas.drawColor(Color.WHITE);
     54 
     55             mPaint.setColor(Color.RED);
     56             canvas.drawLine(0, 0, 100, 100, mPaint);
     57 
     58             mPaint.setColor(Color.GREEN);
     59             canvas.drawCircle(30, 70, 30, mPaint);
     60 
     61             mPaint.setColor(Color.BLUE);
     62             canvas.drawText("Clipping", 100, 30, mPaint);
     63         }
     64 
     65         @Override protected void onDraw(Canvas canvas) {
     66             canvas.drawColor(Color.GRAY);
     67 
     68             canvas.save();
     69             canvas.translate(10, 10);
     70             drawScene(canvas);
     71             canvas.restore();
     72 
     73             canvas.save();
     74             canvas.translate(160, 10);
     75             canvas.clipRect(10, 10, 90, 90);
     76             canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE);
     77             drawScene(canvas);
     78             canvas.restore();
     79 
     80             canvas.save();
     81             canvas.translate(10, 160);
     82             mPath.reset();
     83             canvas.clipPath(mPath); // makes the clip empty
     84             mPath.addCircle(50, 50, 50, Path.Direction.CCW);
     85             canvas.clipPath(mPath, Region.Op.REPLACE);
     86             drawScene(canvas);
     87             canvas.restore();
     88 
     89             canvas.save();
     90             canvas.translate(160, 160);
     91             canvas.clipRect(0, 0, 60, 60);
     92             canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);
     93             drawScene(canvas);
     94             canvas.restore();
     95 
     96             canvas.save();
     97             canvas.translate(10, 310);
     98             canvas.clipRect(0, 0, 60, 60);
     99             canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);
    100             drawScene(canvas);
    101             canvas.restore();
    102 
    103             canvas.save();
    104             canvas.translate(160, 310);
    105             canvas.clipRect(0, 0, 60, 60);
    106             canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);
    107             drawScene(canvas);
    108             canvas.restore();
    109         }
    110     }
    111 }
    112 
    113