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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.graphics.BitmapShader;
24import android.graphics.Canvas;
25import android.graphics.Color;
26import android.graphics.ComposeShader;
27import android.graphics.LinearGradient;
28import android.graphics.Matrix;
29import android.graphics.Paint;
30import android.graphics.PorterDuff;
31import android.graphics.Shader;
32import android.os.Bundle;
33import android.view.View;
34
35@SuppressWarnings({"UnusedDeclaration"})
36public class AdvancedBlendActivity extends Activity {
37    @Override
38    protected void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40
41        setContentView(new ShadersView(this));
42    }
43
44    public static class ShadersView extends View {
45        private BitmapShader mScaledShader;
46        private int mTexWidth;
47        private int mTexHeight;
48        private Paint mPaint;
49        private float mDrawWidth;
50        private float mDrawHeight;
51        private LinearGradient mHorGradient;
52        private ComposeShader mComposeShader;
53        private ComposeShader mCompose2Shader;
54        private ComposeShader mCompose3Shader;
55        private ComposeShader mCompose4Shader;
56        private ComposeShader mCompose5Shader;
57        private ComposeShader mCompose6Shader;
58        private BitmapShader mScaled2Shader;
59
60        public ShadersView(Context c) {
61            super(c);
62
63            Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
64            mTexWidth = texture.getWidth();
65            mTexHeight = texture.getHeight();
66            mDrawWidth = mTexWidth * 2.2f;
67            mDrawHeight = mTexHeight * 1.2f;
68
69            mScaledShader = new BitmapShader(texture, Shader.TileMode.MIRROR,
70                    Shader.TileMode.MIRROR);
71            Matrix m2 = new Matrix();
72            m2.setScale(0.5f, 0.5f);
73            mScaledShader.setLocalMatrix(m2);
74
75            mScaled2Shader = new BitmapShader(texture, Shader.TileMode.MIRROR,
76                    Shader.TileMode.MIRROR);
77            Matrix m3 = new Matrix();
78            m3.setScale(0.1f, 0.1f);
79            mScaled2Shader.setLocalMatrix(m3);
80
81            mHorGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth, 0.0f,
82                    Color.BLACK, Color.WHITE, Shader.TileMode.CLAMP);
83
84            mComposeShader = new ComposeShader(mScaledShader, mHorGradient,
85                    PorterDuff.Mode.DARKEN);
86            mCompose2Shader = new ComposeShader(mScaledShader, mHorGradient,
87                    PorterDuff.Mode.LIGHTEN);
88            mCompose3Shader = new ComposeShader(mScaledShader, mHorGradient,
89                    PorterDuff.Mode.MULTIPLY);
90            mCompose4Shader = new ComposeShader(mScaledShader, mHorGradient,
91                    PorterDuff.Mode.SCREEN);
92            mCompose5Shader = new ComposeShader(mScaledShader, mHorGradient,
93                    PorterDuff.Mode.ADD);
94            mCompose6Shader = new ComposeShader(mHorGradient, mScaledShader,
95                    PorterDuff.Mode.OVERLAY);
96
97            mPaint = new Paint();
98        }
99
100        @Override
101        protected void onDraw(Canvas canvas) {
102            super.onDraw(canvas);
103            canvas.drawRGB(255, 255, 255);
104
105            canvas.save();
106            canvas.translate(40.0f, 40.0f);
107
108            mPaint.setShader(mComposeShader);
109            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
110
111            canvas.translate(0.0f, 40.0f + mDrawHeight);
112            mPaint.setShader(mCompose2Shader);
113            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
114
115            canvas.translate(0.0f, 40.0f + mDrawHeight);
116            mPaint.setShader(mCompose3Shader);
117            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
118
119            canvas.restore();
120
121            canvas.save();
122            canvas.translate(40.0f + mDrawWidth + 40.0f, 40.0f);
123
124            mPaint.setShader(mCompose4Shader);
125            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
126
127            canvas.translate(0.0f, 40.0f + mDrawHeight);
128            mPaint.setShader(mCompose5Shader);
129            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
130
131            canvas.translate(0.0f, 40.0f + mDrawHeight);
132            mPaint.setShader(mCompose6Shader);
133            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
134
135            canvas.restore();
136        }
137    }
138}
139