ShadersActivity.java revision f607bdc167f66b3e7003acaa4736ae46d78c1492
1d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy/*
2d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * Copyright (C) 2010 The Android Open Source Project
3d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy *
4d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * you may not use this file except in compliance with the License.
6d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * You may obtain a copy of the License at
7d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy *
8d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy *
10d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * Unless required by applicable law or agreed to in writing, software
11d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * See the License for the specific language governing permissions and
14d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy * limitations under the License.
15d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy */
16d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
17f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guypackage com.android.test.hwui;
18d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
19d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.app.Activity;
20d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.content.Context;
21d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.Bitmap;
22d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.BitmapFactory;
23d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.BitmapShader;
24d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.Canvas;
25d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.Color;
26d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.LinearGradient;
27d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.Matrix;
28d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.Paint;
29d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.graphics.Shader;
30d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.os.Bundle;
31d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyimport android.view.View;
32d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
33d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy@SuppressWarnings({"UnusedDeclaration"})
34d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guypublic class ShadersActivity extends Activity {
35d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    @Override
36d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    protected void onCreate(Bundle savedInstanceState) {
37d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        super.onCreate(savedInstanceState);
38d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
39d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        setContentView(new ShadersView(this));
40d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    }
41d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
42d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    static class ShadersView extends View {
43d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private BitmapShader mRepeatShader;
44d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private BitmapShader mTranslatedShader;
45d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private BitmapShader mScaledShader;
46d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private int mTexWidth;
47d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private int mTexHeight;
48d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private Paint mPaint;
49d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private float mDrawWidth;
50d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private float mDrawHeight;
51d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private LinearGradient mHorGradient;
52d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private LinearGradient mDiagGradient;
53d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        private LinearGradient mVertGradient;
54d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
55d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        ShadersView(Context c) {
56d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            super(c);
57d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
58d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
59d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mTexWidth = texture.getWidth();
60d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mTexHeight = texture.getHeight();
61d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mDrawWidth = mTexWidth * 2.2f;
62d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mDrawHeight = mTexHeight * 1.2f;
63d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
64d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mRepeatShader = new BitmapShader(texture, Shader.TileMode.REPEAT,
65d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy                    Shader.TileMode.REPEAT);
66d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
67d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mTranslatedShader = new BitmapShader(texture, Shader.TileMode.REPEAT,
68d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy                    Shader.TileMode.REPEAT);
69d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            Matrix m1 = new Matrix();
70d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            m1.setTranslate(mTexWidth / 2.0f, mTexHeight / 2.0f);
71889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy            m1.postRotate(45, 0, 0);
72d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mTranslatedShader.setLocalMatrix(m1);
73d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
74d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mScaledShader = new BitmapShader(texture, Shader.TileMode.MIRROR,
75d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy                    Shader.TileMode.MIRROR);
76d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            Matrix m2 = new Matrix();
77d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            m2.setScale(0.5f, 0.5f);
78d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mScaledShader.setLocalMatrix(m2);
79d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
80d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mHorGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth, 0.0f,
81ae5575b3421c8fbe590ab046d7d5f2b36ecfd821Romain Guy                    Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
82d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
83d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 1.5f, mDrawHeight,
84d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy                    Color.BLUE, Color.MAGENTA, Shader.TileMode.CLAMP);
85d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
86d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f,
87d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy                    Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR);
88d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
89d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint = new Paint();
90d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        }
91d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
92d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        @Override
93d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        protected void onDraw(Canvas canvas) {
94d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            super.onDraw(canvas);
95c0ac193b9415680f0a69e20a3f5f22d16f8053beRomain Guy            //canvas.drawRGB(255, 255, 255);
96d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
97d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            // Bitmap shaders
98d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.save();
99d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.translate(40.0f, 40.0f);
100d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
101d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint.setShader(mRepeatShader);
102d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
103d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
104d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.translate(0.0f, 40.0f + mDrawHeight);
105d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint.setShader(mTranslatedShader);
106d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
107d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
108d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.translate(0.0f, 40.0f + mDrawHeight);
109d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint.setShader(mScaledShader);
110d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
111d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
112d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.restore();
113d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
114d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            // Gradients
115d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.save();
116d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.translate(40.0f + mDrawWidth + 40.0f, 40.0f);
117d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
118d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint.setShader(mHorGradient);
119d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
120d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
121d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.translate(0.0f, 40.0f + mDrawHeight);
122d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint.setShader(mDiagGradient);
123d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
124d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
125d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.translate(0.0f, 40.0f + mDrawHeight);
126d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            mPaint.setShader(mVertGradient);
127d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
128d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
129d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy            canvas.restore();
130d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        }
131d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    }
132d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy}
133