Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2007 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.Bitmap;
     22 import android.graphics.BitmapShader;
     23 import android.graphics.Canvas;
     24 import android.graphics.Color;
     25 import android.graphics.Matrix;
     26 import android.graphics.Paint;
     27 import android.graphics.PorterDuff;
     28 import android.graphics.PorterDuffXfermode;
     29 import android.graphics.RectF;
     30 import android.graphics.Shader;
     31 import android.graphics.Xfermode;
     32 import android.os.Bundle;
     33 import android.view.View;
     34 
     35 public class Xfermodes extends GraphicsActivity {
     36 
     37     // create a bitmap with a circle, used for the "dst" image
     38     static Bitmap makeDst(int w, int h) {
     39         Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     40         Canvas c = new Canvas(bm);
     41         Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
     42 
     43         p.setColor(0xFFFFCC44);
     44         c.drawOval(new RectF(0, 0, w*3/4, h*3/4), p);
     45         return bm;
     46     }
     47 
     48     // create a bitmap with a rect, used for the "src" image
     49     static Bitmap makeSrc(int w, int h) {
     50         Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     51         Canvas c = new Canvas(bm);
     52         Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
     53 
     54         p.setColor(0xFF66AAFF);
     55         c.drawRect(w/3, h/3, w*19/20, h*19/20, p);
     56         return bm;
     57     }
     58 
     59     @Override
     60     protected void onCreate(Bundle savedInstanceState) {
     61         super.onCreate(savedInstanceState);
     62         setContentView(new SampleView(this));
     63     }
     64 
     65     private static class SampleView extends View {
     66         private static final int W = 64;
     67         private static final int H = 64;
     68         private static final int ROW_MAX = 4;   // number of samples per row
     69 
     70         private Bitmap mSrcB;
     71         private Bitmap mDstB;
     72         private Shader mBG;     // background checker-board pattern
     73 
     74         private static final Xfermode[] sModes = {
     75             new PorterDuffXfermode(PorterDuff.Mode.CLEAR),
     76             new PorterDuffXfermode(PorterDuff.Mode.SRC),
     77             new PorterDuffXfermode(PorterDuff.Mode.DST),
     78             new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),
     79             new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),
     80             new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),
     81             new PorterDuffXfermode(PorterDuff.Mode.DST_IN),
     82             new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),
     83             new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),
     84             new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),
     85             new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),
     86             new PorterDuffXfermode(PorterDuff.Mode.XOR),
     87             new PorterDuffXfermode(PorterDuff.Mode.DARKEN),
     88             new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),
     89             new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),
     90             new PorterDuffXfermode(PorterDuff.Mode.SCREEN)
     91         };
     92 
     93         private static final String[] sLabels = {
     94             "Clear", "Src", "Dst", "SrcOver",
     95             "DstOver", "SrcIn", "DstIn", "SrcOut",
     96             "DstOut", "SrcATop", "DstATop", "Xor",
     97             "Darken", "Lighten", "Multiply", "Screen"
     98         };
     99 
    100         public SampleView(Context context) {
    101             super(context);
    102 
    103             mSrcB = makeSrc(W, H);
    104             mDstB = makeDst(W, H);
    105 
    106             // make a ckeckerboard pattern
    107             Bitmap bm = Bitmap.createBitmap(new int[] { 0xFFFFFFFF, 0xFFCCCCCC,
    108                                             0xFFCCCCCC, 0xFFFFFFFF }, 2, 2,
    109                                             Bitmap.Config.RGB_565);
    110             mBG = new BitmapShader(bm,
    111                                    Shader.TileMode.REPEAT,
    112                                    Shader.TileMode.REPEAT);
    113             Matrix m = new Matrix();
    114             m.setScale(6, 6);
    115             mBG.setLocalMatrix(m);
    116         }
    117 
    118         @Override protected void onDraw(Canvas canvas) {
    119             canvas.drawColor(Color.WHITE);
    120 
    121             Paint labelP = new Paint(Paint.ANTI_ALIAS_FLAG);
    122             labelP.setTextAlign(Paint.Align.CENTER);
    123 
    124             Paint paint = new Paint();
    125             paint.setFilterBitmap(false);
    126 
    127             canvas.translate(15, 35);
    128 
    129             int x = 0;
    130             int y = 0;
    131             for (int i = 0; i < sModes.length; i++) {
    132                 // draw the border
    133                 paint.setStyle(Paint.Style.STROKE);
    134                 paint.setShader(null);
    135                 canvas.drawRect(x - 0.5f, y - 0.5f,
    136                                 x + W + 0.5f, y + H + 0.5f, paint);
    137 
    138                 // draw the checker-board pattern
    139                 paint.setStyle(Paint.Style.FILL);
    140                 paint.setShader(mBG);
    141                 canvas.drawRect(x, y, x + W, y + H, paint);
    142 
    143                 // draw the src/dst example into our offscreen bitmap
    144                 int sc = canvas.saveLayer(x, y, x + W, y + H, null,
    145                                           Canvas.MATRIX_SAVE_FLAG |
    146                                           Canvas.CLIP_SAVE_FLAG |
    147                                           Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
    148                                           Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
    149                                           Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    150                 canvas.translate(x, y);
    151                 canvas.drawBitmap(mDstB, 0, 0, paint);
    152                 paint.setXfermode(sModes[i]);
    153                 canvas.drawBitmap(mSrcB, 0, 0, paint);
    154                 paint.setXfermode(null);
    155                 canvas.restoreToCount(sc);
    156 
    157                 // draw the label
    158                 canvas.drawText(sLabels[i],
    159                                 x + W/2, y - labelP.getTextSize()/2, labelP);
    160 
    161                 x += W + 10;
    162 
    163                 // wrap around when we've drawn enough for one row
    164                 if ((i % ROW_MAX) == ROW_MAX - 1) {
    165                     x = 0;
    166                     y += H + 30;
    167                 }
    168             }
    169         }
    170     }
    171 }
    172 
    173