ShadersActivity.java revision f607bdc167f66b3e7003acaa4736ae46d78c1492
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.LinearGradient;
27import android.graphics.Matrix;
28import android.graphics.Paint;
29import android.graphics.Shader;
30import android.os.Bundle;
31import android.view.View;
32
33@SuppressWarnings({"UnusedDeclaration"})
34public class ShadersActivity extends Activity {
35    @Override
36    protected void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38
39        setContentView(new ShadersView(this));
40    }
41
42    static class ShadersView extends View {
43        private BitmapShader mRepeatShader;
44        private BitmapShader mTranslatedShader;
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 LinearGradient mDiagGradient;
53        private LinearGradient mVertGradient;
54
55        ShadersView(Context c) {
56            super(c);
57
58            Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
59            mTexWidth = texture.getWidth();
60            mTexHeight = texture.getHeight();
61            mDrawWidth = mTexWidth * 2.2f;
62            mDrawHeight = mTexHeight * 1.2f;
63
64            mRepeatShader = new BitmapShader(texture, Shader.TileMode.REPEAT,
65                    Shader.TileMode.REPEAT);
66
67            mTranslatedShader = new BitmapShader(texture, Shader.TileMode.REPEAT,
68                    Shader.TileMode.REPEAT);
69            Matrix m1 = new Matrix();
70            m1.setTranslate(mTexWidth / 2.0f, mTexHeight / 2.0f);
71            m1.postRotate(45, 0, 0);
72            mTranslatedShader.setLocalMatrix(m1);
73
74            mScaledShader = new BitmapShader(texture, Shader.TileMode.MIRROR,
75                    Shader.TileMode.MIRROR);
76            Matrix m2 = new Matrix();
77            m2.setScale(0.5f, 0.5f);
78            mScaledShader.setLocalMatrix(m2);
79
80            mHorGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth, 0.0f,
81                    Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
82
83            mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 1.5f, mDrawHeight,
84                    Color.BLUE, Color.MAGENTA, Shader.TileMode.CLAMP);
85
86            mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f,
87                    Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR);
88
89            mPaint = new Paint();
90        }
91
92        @Override
93        protected void onDraw(Canvas canvas) {
94            super.onDraw(canvas);
95            //canvas.drawRGB(255, 255, 255);
96
97            // Bitmap shaders
98            canvas.save();
99            canvas.translate(40.0f, 40.0f);
100
101            mPaint.setShader(mRepeatShader);
102            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
103
104            canvas.translate(0.0f, 40.0f + mDrawHeight);
105            mPaint.setShader(mTranslatedShader);
106            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
107
108            canvas.translate(0.0f, 40.0f + mDrawHeight);
109            mPaint.setShader(mScaledShader);
110            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
111
112            canvas.restore();
113
114            // Gradients
115            canvas.save();
116            canvas.translate(40.0f + mDrawWidth + 40.0f, 40.0f);
117
118            mPaint.setShader(mHorGradient);
119            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
120
121            canvas.translate(0.0f, 40.0f + mDrawHeight);
122            mPaint.setShader(mDiagGradient);
123            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
124
125            canvas.translate(0.0f, 40.0f + mDrawHeight);
126            mPaint.setShader(mVertGradient);
127            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
128
129            canvas.restore();
130        }
131    }
132}
133