blob: 2db1071899a4a79b25454878cd9a2d829133d32a [file] [log] [blame]
Romain Guyd27977d2010-07-14 19:18:51 -07001/*
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
Romain Guyf607bdc2010-09-10 19:20:06 -070017package com.android.test.hwui;
Romain Guyd27977d2010-07-14 19:18:51 -070018
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);
Romain Guy889f8d12010-07-29 14:37:42 -070071 m1.postRotate(45, 0, 0);
Romain Guyd27977d2010-07-14 19:18:51 -070072 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
Romain Guy0bb56672010-10-01 00:25:02 -070080 mHorGradient = new LinearGradient(0.0f, 0.0f, 1.0f, 0.0f,
Romain Guyae5575b2010-07-29 18:48:04 -070081 Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
Romain Guy0bb56672010-10-01 00:25:02 -070082 Matrix m3 = new Matrix();
83 m3.setScale(mDrawHeight, 1.0f);
84 m3.postRotate(-90.0f);
85 m3.postTranslate(0.0f, mDrawHeight);
86 mHorGradient.setLocalMatrix(m3);
Romain Guyd27977d2010-07-14 19:18:51 -070087
88 mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 1.5f, mDrawHeight,
89 Color.BLUE, Color.MAGENTA, Shader.TileMode.CLAMP);
90
91 mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f,
92 Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR);
93
94 mPaint = new Paint();
95 }
96
97 @Override
98 protected void onDraw(Canvas canvas) {
99 super.onDraw(canvas);
Romain Guyc0ac1932010-07-19 18:43:02 -0700100 //canvas.drawRGB(255, 255, 255);
Romain Guyd27977d2010-07-14 19:18:51 -0700101
102 // Bitmap shaders
103 canvas.save();
104 canvas.translate(40.0f, 40.0f);
105
106 mPaint.setShader(mRepeatShader);
107 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
108
109 canvas.translate(0.0f, 40.0f + mDrawHeight);
110 mPaint.setShader(mTranslatedShader);
111 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
112
113 canvas.translate(0.0f, 40.0f + mDrawHeight);
114 mPaint.setShader(mScaledShader);
115 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
116
117 canvas.restore();
118
119 // Gradients
120 canvas.save();
121 canvas.translate(40.0f + mDrawWidth + 40.0f, 40.0f);
122
123 mPaint.setShader(mHorGradient);
124 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
125
126 canvas.translate(0.0f, 40.0f + mDrawHeight);
127 mPaint.setShader(mDiagGradient);
128 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
129
130 canvas.translate(0.0f, 40.0f + mDrawHeight);
131 mPaint.setShader(mVertGradient);
132 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint);
133
134 canvas.restore();
135 }
136 }
137}