1 /* 2 * Copyright (C) 2010 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.android.test.hwui; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.graphics.Canvas; 22 import android.graphics.Paint; 23 import android.graphics.PorterDuff; 24 import android.graphics.PorterDuffXfermode; 25 import android.os.Bundle; 26 import android.view.View; 27 28 @SuppressWarnings({"UnusedDeclaration"}) 29 public class LayersActivity extends Activity { 30 @Override onCreate(Bundle savedInstanceState)31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(new LayersView(this)); 34 } 35 36 public static class LayersView extends View { 37 private Paint mLayerPaint; 38 private final Paint mRectPaint; 39 LayersView(Context c)40 public LayersView(Context c) { 41 super(c); 42 43 mLayerPaint = new Paint(); 44 mRectPaint = new Paint(); 45 } 46 47 @Override onDraw(Canvas canvas)48 protected void onDraw(Canvas canvas) { 49 super.onDraw(canvas); 50 51 canvas.translate(140.0f, 100.0f); 52 53 //canvas.drawRGB(255, 255, 255); 54 55 int count = canvas.saveLayer(0.0f, 0.0f, 200.0f, 100.0f, mLayerPaint, 56 Canvas.ALL_SAVE_FLAG); 57 58 mRectPaint.setColor(0xffff0000); 59 canvas.drawRect(0.0f, 0.0f, 200.0f, 100.0f, mRectPaint); 60 61 canvas.restoreToCount(count); 62 63 canvas.translate(0.0f, 125.0f); 64 65 count = canvas.saveLayer(0.0f, 0.0f, 200.0f, 100.0f, mLayerPaint, 66 Canvas.ALL_SAVE_FLAG); 67 68 mRectPaint.setColor(0xff00ff00); 69 mRectPaint.setAlpha(50); 70 canvas.drawRect(0.0f, 0.0f, 200.0f, 100.0f, mRectPaint); 71 72 canvas.restoreToCount(count); 73 74 canvas.translate(25.0f, 125.0f); 75 76 mRectPaint.setColor(0xff0000ff); 77 mRectPaint.setAlpha(255); 78 canvas.drawRect(0.0f, 0.0f, 100.0f, 50.0f, mRectPaint); 79 80 mLayerPaint.setAlpha(127); 81 mLayerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); 82 count = canvas.saveLayer(50.0f, 25.0f, 150.0f, 75.0f, mLayerPaint, 83 Canvas.ALL_SAVE_FLAG); 84 85 mRectPaint.setColor(0xffff0000); 86 canvas.drawRect(50.0f, 25.0f, 150.0f, 75.0f, mRectPaint); 87 88 canvas.restoreToCount(count); 89 90 canvas.translate(0.0f, 125.0f); 91 92 mRectPaint.setColor(0xff0000ff); 93 mRectPaint.setAlpha(255); 94 canvas.drawRect(0.0f, 0.0f, 100.0f, 50.0f, mRectPaint); 95 96 mLayerPaint.setColor(0xffff0000); 97 mLayerPaint.setAlpha(127); 98 mLayerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); 99 count = canvas.saveLayer(50.0f, 25.0f, 150.0f, 75.0f, mLayerPaint, 100 Canvas.ALL_SAVE_FLAG); 101 102 mRectPaint.setColor(0xffff0000); 103 canvas.drawRect(50.0f, 25.0f, 150.0f, 75.0f, mRectPaint); 104 105 canvas.restoreToCount(count); 106 107 mLayerPaint = new Paint(); 108 } 109 } 110 } 111