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 import static android.graphics.PorterDuff.Mode;
29 
30 @SuppressWarnings({"UnusedDeclaration"})
31 public class XfermodeActivity extends Activity {
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         setContentView(new XfermodesView(this));
37     }
38 
39     static class XfermodesView extends View {
40         private final Paint mBluePaint;
41         private final Paint mRedPaint;
42 
XfermodesView(Context c)43         XfermodesView(Context c) {
44             super(c);
45 
46             mBluePaint = new Paint();
47             mBluePaint.setColor(0xff0000ff);
48 
49             mRedPaint = new Paint();
50             mRedPaint.setColor(0x7fff0000);
51         }
52 
53         @Override
onDraw(Canvas canvas)54         protected void onDraw(Canvas canvas) {
55             super.onDraw(canvas);
56             //canvas.drawRGB(255, 255, 255);
57 
58             canvas.translate(100.0f, 100.0f);
59 
60             // SRC modes
61             canvas.save();
62 
63             drawRects(canvas, Mode.SRC_OVER);
64             canvas.translate(0.0f, 100.0f);
65 
66             drawRects(canvas, Mode.SRC_IN);
67             canvas.translate(0.0f, 100.0f);
68 
69             drawRects(canvas, Mode.SRC_OUT);
70             canvas.translate(0.0f, 100.0f);
71 
72             drawRects(canvas, Mode.SRC_ATOP);
73             canvas.translate(0.0f, 100.0f);
74 
75             drawRects(canvas, Mode.SRC);
76 
77             canvas.restore();
78 
79             canvas.translate(100.0f, 0.0f);
80 
81             // DST modes
82             canvas.save();
83 
84             drawRects(canvas, Mode.DST_OVER);
85             canvas.translate(0.0f, 100.0f);
86 
87             drawRects(canvas, Mode.DST_IN);
88             canvas.translate(0.0f, 100.0f);
89 
90             drawRects(canvas, Mode.DST_OUT);
91             canvas.translate(0.0f, 100.0f);
92 
93             drawRects(canvas, Mode.DST_ATOP);
94             canvas.translate(0.0f, 100.0f);
95 
96             drawRects(canvas, Mode.DST);
97 
98             canvas.restore();
99 
100             canvas.translate(100.0f, 0.0f);
101 
102             // Other modes
103             canvas.save();
104 
105             drawRects(canvas, Mode.CLEAR);
106             canvas.translate(0.0f, 100.0f);
107 
108             drawRects(canvas, Mode.XOR);
109 
110             canvas.translate(0.0f, 100.0f);
111 
112             mBluePaint.setAlpha(127);
113             canvas.drawRect(0.0f, 0.0f, 50.0f, 50.0f, mBluePaint);
114 
115             canvas.translate(0.0f, 100.0f);
116 
117             mBluePaint.setAlpha(10);
118             mBluePaint.setColor(0x7f0000ff);
119             canvas.drawRect(0.0f, 0.0f, 50.0f, 50.0f, mBluePaint);
120 
121             mBluePaint.setColor(0xff0000ff);
122             mBluePaint.setAlpha(255);
123 
124             canvas.restore();
125         }
126 
drawRects(Canvas canvas, PorterDuff.Mode mode)127         private void drawRects(Canvas canvas, PorterDuff.Mode mode) {
128             canvas.drawRect(0.0f, 0.0f, 50.0f, 50.0f, mBluePaint);
129 
130             canvas.save();
131             canvas.translate(25.0f, 25.0f);
132             mRedPaint.setXfermode(new PorterDuffXfermode(mode));
133             canvas.drawRect(0.0f, 0.0f, 50.0f, 50.0f, mRedPaint);
134             canvas.restore();
135         }
136     }
137 }
138