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 
18 package com.android.test.hwui;
19 
20 import android.app.Activity;
21 import android.content.Context;
22 import android.graphics.Bitmap;
23 import android.graphics.BitmapFactory;
24 import android.graphics.Canvas;
25 import android.graphics.Color;
26 import android.graphics.LinearGradient;
27 import android.graphics.Paint;
28 import android.graphics.PorterDuff;
29 import android.graphics.PorterDuffXfermode;
30 import android.graphics.Shader;
31 import android.os.Bundle;
32 import android.view.View;
33 
34 @SuppressWarnings({"UnusedDeclaration"})
35 public class FramebufferBlendActivity extends Activity {
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39 
40         setContentView(new BlendView(this));
41     }
42 
43     static class BlendView extends View {
44         private int mTexWidth;
45         private int mTexHeight;
46         private Paint mPaint;
47         private LinearGradient mHorGradient;
48         private Bitmap mTexture;
49 
BlendView(Context c)50         BlendView(Context c) {
51             super(c);
52 
53             mTexture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
54             mTexWidth = mTexture.getWidth();
55             mTexHeight = mTexture.getHeight();
56 
57             mHorGradient = new LinearGradient(0.0f, 0.0f, mTexWidth, 0.0f,
58                     Color.BLACK, Color.WHITE, Shader.TileMode.CLAMP);
59 
60             mPaint = new Paint();
61         }
62 
63         @Override
onDraw(Canvas canvas)64         protected void onDraw(Canvas canvas) {
65             super.onDraw(canvas);
66             canvas.drawRGB(255, 255, 255);
67 
68             canvas.save();
69             canvas.translate(40.0f, 40.0f);
70 
71             drawBlendedBitmap(canvas, PorterDuff.Mode.DARKEN);
72             drawBlendedBitmap(canvas, PorterDuff.Mode.LIGHTEN);
73             drawBlendedBitmap(canvas, PorterDuff.Mode.MULTIPLY);
74 
75             canvas.restore();
76 
77             canvas.save();
78             canvas.translate(40.0f + mTexWidth + 40.0f, 40.0f);
79 
80             drawBlendedBitmap(canvas, PorterDuff.Mode.SCREEN);
81             drawBlendedBitmap(canvas, PorterDuff.Mode.ADD);
82             drawBlendedBitmapInverse(canvas, PorterDuff.Mode.OVERLAY);
83 
84             canvas.restore();
85         }
86 
drawBlendedBitmap(Canvas canvas, PorterDuff.Mode mode)87         private void drawBlendedBitmap(Canvas canvas, PorterDuff.Mode mode) {
88             mPaint.setShader(null);
89             mPaint.setXfermode(null);
90             canvas.drawBitmap(mTexture, 0.0f, 0.0f, mPaint);
91 
92             mPaint.setShader(mHorGradient);
93             mPaint.setXfermode(new PorterDuffXfermode(mode));
94             canvas.drawRect(0.0f, 0.0f, mTexWidth, mTexHeight, mPaint);
95 
96             canvas.translate(0.0f, 40.0f + mTexHeight);
97         }
98 
drawBlendedBitmapInverse(Canvas canvas, PorterDuff.Mode mode)99         private void drawBlendedBitmapInverse(Canvas canvas, PorterDuff.Mode mode) {
100             mPaint.setXfermode(null);
101             mPaint.setShader(mHorGradient);
102             canvas.drawRect(0.0f, 0.0f, mTexWidth, mTexHeight, mPaint);
103 
104             mPaint.setXfermode(new PorterDuffXfermode(mode));
105             mPaint.setShader(null);
106             canvas.drawBitmap(mTexture, 0.0f, 0.0f, mPaint);
107 
108             canvas.translate(0.0f, 40.0f + mTexHeight);
109         }
110     }
111 }
112