blob: fbe7856f32ec80a687de9488cd8777fedf643b5f [file] [log] [blame]
Romain Guy0bb56672010-10-01 00:25:02 -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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
Romain Guy14830942010-10-07 15:07:45 -070021import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.graphics.BitmapShader;
Romain Guy0bb56672010-10-01 00:25:02 -070024import android.graphics.Canvas;
25import android.graphics.LinearGradient;
Romain Guy14830942010-10-07 15:07:45 -070026import android.graphics.RadialGradient;
Romain Guy0bb56672010-10-01 00:25:02 -070027import android.graphics.Matrix;
28import android.graphics.Paint;
29import android.graphics.Shader;
Romain Guy14830942010-10-07 15:07:45 -070030import android.graphics.SweepGradient;
Romain Guy0bb56672010-10-01 00:25:02 -070031import android.os.Bundle;
Romain Guye3095e02010-10-06 16:57:29 -070032import android.view.Gravity;
Romain Guy0bb56672010-10-01 00:25:02 -070033import android.view.View;
Romain Guye3095e02010-10-06 16:57:29 -070034import android.widget.FrameLayout;
35import android.widget.SeekBar;
Romain Guy0bb56672010-10-01 00:25:02 -070036
37@SuppressWarnings({"UnusedDeclaration"})
38public class GradientsActivity extends Activity {
39 @Override
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42
Romain Guye3095e02010-10-06 16:57:29 -070043 final FrameLayout layout = new FrameLayout(this);
Romain Guy14830942010-10-07 15:07:45 -070044
Romain Guye3095e02010-10-06 16:57:29 -070045 final ShadersView shadersView = new ShadersView(this);
46 final GradientView gradientView = new GradientView(this);
Romain Guy14830942010-10-07 15:07:45 -070047 final RadialGradientView radialGradientView = new RadialGradientView(this);
48 final SweepGradientView sweepGradientView = new SweepGradientView(this);
49 final BitmapView bitmapView = new BitmapView(this);
50
Romain Guye3095e02010-10-06 16:57:29 -070051 final SeekBar rotateView = new SeekBar(this);
52 rotateView.setMax(360);
53 rotateView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Romain Guye3095e02010-10-06 16:57:29 -070054 public void onStopTrackingTouch(SeekBar seekBar) {
55 }
56
Romain Guye3095e02010-10-06 16:57:29 -070057 public void onStartTrackingTouch(SeekBar seekBar) {
58 }
59
Romain Guye3095e02010-10-06 16:57:29 -070060 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Romain Guy14830942010-10-07 15:07:45 -070061 gradientView.setRotationY((float) progress);
62 radialGradientView.setRotationX((float) progress);
63 sweepGradientView.setRotationY((float) progress);
64 bitmapView.setRotationX((float) progress);
Romain Guye3095e02010-10-06 16:57:29 -070065 }
66 });
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -080067
Romain Guye3095e02010-10-06 16:57:29 -070068 layout.addView(shadersView);
69 layout.addView(gradientView, new FrameLayout.LayoutParams(
70 200, 200, Gravity.CENTER));
Romain Guy14830942010-10-07 15:07:45 -070071
72 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER);
73 lp.setMargins(220, 0, 0, 0);
74 layout.addView(radialGradientView, lp);
75
76 lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER);
77 lp.setMargins(440, 0, 0, 0);
78 layout.addView(sweepGradientView, lp);
79
80 lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER);
81 lp.setMargins(220, -220, 0, 0);
82 layout.addView(bitmapView, lp);
83
Romain Guye3095e02010-10-06 16:57:29 -070084 layout.addView(rotateView, new FrameLayout.LayoutParams(
85 300, FrameLayout.LayoutParams.WRAP_CONTENT,
86 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
87
88 setContentView(layout);
89 }
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -080090
Romain Guy14830942010-10-07 15:07:45 -070091 static class BitmapView extends View {
92 private final Paint mPaint;
93
94 BitmapView(Context c) {
95 super(c);
96
97 Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
98 BitmapShader shader = new BitmapShader(texture, Shader.TileMode.REPEAT,
99 Shader.TileMode.REPEAT);
100 mPaint = new Paint();
101 mPaint.setShader(shader);
102 }
103
104 @Override
105 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
106 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
107 setMeasuredDimension(200, 200);
108 }
109
110 @Override
111 protected void onDraw(Canvas canvas) {
112 super.onDraw(canvas);
113 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint);
114 }
115 }
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800116
Romain Guye3095e02010-10-06 16:57:29 -0700117 static class GradientView extends View {
118 private final Paint mPaint;
119
120 GradientView(Context c) {
121 super(c);
122
123 LinearGradient gradient = new LinearGradient(0, 0, 200, 0, 0xFF000000, 0,
124 Shader.TileMode.CLAMP);
125 mPaint = new Paint();
126 mPaint.setShader(gradient);
127 }
128
129 @Override
130 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
131 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
132 setMeasuredDimension(200, 200);
133 }
134
135 @Override
136 protected void onDraw(Canvas canvas) {
137 super.onDraw(canvas);
138 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint);
139 }
Romain Guy0bb56672010-10-01 00:25:02 -0700140 }
141
Romain Guy14830942010-10-07 15:07:45 -0700142 static class RadialGradientView extends View {
143 private final Paint mPaint;
144
145 RadialGradientView(Context c) {
146 super(c);
147
148 RadialGradient gradient = new RadialGradient(0.0f, 0.0f, 100.0f, 0xff000000, 0xffffffff,
149 Shader.TileMode.MIRROR);
150 mPaint = new Paint();
151 mPaint.setShader(gradient);
152 }
153
154 @Override
155 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
156 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
157 setMeasuredDimension(200, 200);
158 }
159
160 @Override
161 protected void onDraw(Canvas canvas) {
162 super.onDraw(canvas);
163 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint);
164 }
165 }
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800166
Romain Guy14830942010-10-07 15:07:45 -0700167 static class SweepGradientView extends View {
168 private final Paint mPaint;
169
170 SweepGradientView(Context c) {
171 super(c);
172
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800173 SweepGradient gradient = new SweepGradient(100.0f, 100.0f, 0xff000000, 0xffffffff);
Romain Guy14830942010-10-07 15:07:45 -0700174 mPaint = new Paint();
175 mPaint.setShader(gradient);
176 }
177
178 @Override
179 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
180 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
181 setMeasuredDimension(200, 200);
182 }
183
184 @Override
185 protected void onDraw(Canvas canvas) {
186 super.onDraw(canvas);
187 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint);
188 }
189 }
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800190
Romain Guy0bb56672010-10-01 00:25:02 -0700191 static class ShadersView extends View {
192 private final Paint mPaint;
193 private final float mDrawWidth;
194 private final float mDrawHeight;
195 private final LinearGradient mGradient;
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700196 private final LinearGradient mGradientStops;
Romain Guy0bb56672010-10-01 00:25:02 -0700197 private final Matrix mMatrix;
198
199 ShadersView(Context c) {
200 super(c);
201
202 mDrawWidth = 200;
203 mDrawHeight = 200;
204
205 mGradient = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700206 mGradientStops = new LinearGradient(0, 0, 0, 1,
207 new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }, null, Shader.TileMode.CLAMP);
208
Romain Guy0bb56672010-10-01 00:25:02 -0700209 mMatrix = new Matrix();
210
211 mPaint = new Paint();
212 }
213
214 @Override
215 protected void onDraw(Canvas canvas) {
216 super.onDraw(canvas);
217 canvas.drawRGB(255, 255, 255);
218
219 // Gradients
220 canvas.save();
221 float top = 40.0f;
222 float right = 40.0f + mDrawWidth;
223 float left = 40.0f;
224 float bottom = 40.0f + mDrawHeight;
225
Romain Guy0bb56672010-10-01 00:25:02 -0700226 mMatrix.setScale(1, mDrawWidth);
227 mMatrix.postRotate(90);
228 mMatrix.postTranslate(right, top);
229 mGradient.setLocalMatrix(mMatrix);
Leon Scroggins IIIab87983a2014-07-09 16:44:35 -0400230 mPaint.setShader(mGradient);
Romain Guy0bb56672010-10-01 00:25:02 -0700231 canvas.drawRect(right - mDrawWidth, top, right, top + mDrawHeight, mPaint);
232
233 top += 40.0f + mDrawHeight;
234 bottom += 40.0f + mDrawHeight;
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800235
Romain Guy0bb56672010-10-01 00:25:02 -0700236 mMatrix.setScale(1, mDrawHeight);
237 mMatrix.postTranslate(left, top);
238 mGradient.setLocalMatrix(mMatrix);
Leon Scroggins IIIab87983a2014-07-09 16:44:35 -0400239 mPaint.setShader(mGradient);
Romain Guy0bb56672010-10-01 00:25:02 -0700240 canvas.drawRect(left, top, right, top + mDrawHeight, mPaint);
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800241
Romain Guy0bb56672010-10-01 00:25:02 -0700242 left += 40.0f + mDrawWidth;
243 right += 40.0f + mDrawWidth;
244 top -= 40.0f + mDrawHeight;
245 bottom -= 40.0f + mDrawHeight;
246
247 mMatrix.setScale(1, mDrawHeight);
248 mMatrix.postRotate(180);
249 mMatrix.postTranslate(left, bottom);
250 mGradient.setLocalMatrix(mMatrix);
Leon Scroggins IIIab87983a2014-07-09 16:44:35 -0400251 mPaint.setShader(mGradient);
Romain Guy0bb56672010-10-01 00:25:02 -0700252 canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint);
253
254 top += 40.0f + mDrawHeight;
255 bottom += 40.0f + mDrawHeight;
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800256
Romain Guy0bb56672010-10-01 00:25:02 -0700257 mMatrix.setScale(1, mDrawWidth);
258 mMatrix.postRotate(-90);
259 mMatrix.postTranslate(left, top);
260 mGradient.setLocalMatrix(mMatrix);
Leon Scroggins IIIab87983a2014-07-09 16:44:35 -0400261 mPaint.setShader(mGradient);
Romain Guy0bb56672010-10-01 00:25:02 -0700262 canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint);
Xavier Ducrohetec31a7f2010-12-14 14:58:59 -0800263
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700264 right = left + mDrawWidth;
265 left = 40.0f;
266 top = bottom + 20.0f;
267 bottom = top + 50.0f;
268
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700269 mMatrix.setScale(1, mDrawWidth);
270 mMatrix.postRotate(90);
271 mMatrix.postTranslate(right, top);
272 mGradientStops.setLocalMatrix(mMatrix);
Leon Scroggins IIIab87983a2014-07-09 16:44:35 -0400273 mPaint.setShader(mGradientStops);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700274 canvas.drawRect(left, top, right, bottom, mPaint);
275
Romain Guy0bb56672010-10-01 00:25:02 -0700276 canvas.restore();
277 }
278 }
279}