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 17 package com.android.test.hwui; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.graphics.BitmapShader; 24 import android.graphics.Canvas; 25 import android.graphics.LinearGradient; 26 import android.graphics.RadialGradient; 27 import android.graphics.Matrix; 28 import android.graphics.Paint; 29 import android.graphics.Shader; 30 import android.graphics.SweepGradient; 31 import android.os.Bundle; 32 import android.view.Gravity; 33 import android.view.View; 34 import android.widget.FrameLayout; 35 import android.widget.SeekBar; 36 37 @SuppressWarnings({"UnusedDeclaration"}) 38 public class GradientsActivity extends Activity { 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 43 final FrameLayout layout = new FrameLayout(this); 44 45 final ShadersView shadersView = new ShadersView(this); 46 final GradientView gradientView = new GradientView(this); 47 final RadialGradientView radialGradientView = new RadialGradientView(this); 48 final SweepGradientView sweepGradientView = new SweepGradientView(this); 49 final BitmapView bitmapView = new BitmapView(this); 50 51 final SeekBar rotateView = new SeekBar(this); 52 rotateView.setMax(360); 53 rotateView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 54 public void onStopTrackingTouch(SeekBar seekBar) { 55 } 56 57 public void onStartTrackingTouch(SeekBar seekBar) { 58 } 59 60 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 61 gradientView.setRotationY((float) progress); 62 radialGradientView.setRotationX((float) progress); 63 sweepGradientView.setRotationY((float) progress); 64 bitmapView.setRotationX((float) progress); 65 } 66 }); 67 68 layout.addView(shadersView); 69 layout.addView(gradientView, new FrameLayout.LayoutParams( 70 200, 200, Gravity.CENTER)); 71 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 84 layout.addView(rotateView, new FrameLayout.LayoutParams( 85 300, FrameLayout.LayoutParams.WRAP_CONTENT, 86 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)); 87 88 setContentView(layout); 89 } 90 91 static class BitmapView extends View { 92 private final Paint mPaint; 93 BitmapView(Context c)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 onMeasure(int widthMeasureSpec, int heightMeasureSpec)105 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 106 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 107 setMeasuredDimension(200, 200); 108 } 109 110 @Override onDraw(Canvas canvas)111 protected void onDraw(Canvas canvas) { 112 super.onDraw(canvas); 113 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 114 } 115 } 116 117 static class GradientView extends View { 118 private final Paint mPaint; 119 GradientView(Context c)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 onMeasure(int widthMeasureSpec, int heightMeasureSpec)130 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 131 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 132 setMeasuredDimension(200, 200); 133 } 134 135 @Override onDraw(Canvas canvas)136 protected void onDraw(Canvas canvas) { 137 super.onDraw(canvas); 138 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 139 } 140 } 141 142 static class RadialGradientView extends View { 143 private final Paint mPaint; 144 RadialGradientView(Context c)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 onMeasure(int widthMeasureSpec, int heightMeasureSpec)155 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 156 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 157 setMeasuredDimension(200, 200); 158 } 159 160 @Override onDraw(Canvas canvas)161 protected void onDraw(Canvas canvas) { 162 super.onDraw(canvas); 163 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 164 } 165 } 166 167 static class SweepGradientView extends View { 168 private final Paint mPaint; 169 SweepGradientView(Context c)170 SweepGradientView(Context c) { 171 super(c); 172 173 SweepGradient gradient = new SweepGradient(100.0f, 100.0f, 0xff000000, 0xffffffff); 174 mPaint = new Paint(); 175 mPaint.setShader(gradient); 176 } 177 178 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)179 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 180 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 181 setMeasuredDimension(200, 200); 182 } 183 184 @Override onDraw(Canvas canvas)185 protected void onDraw(Canvas canvas) { 186 super.onDraw(canvas); 187 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 188 } 189 } 190 191 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; 196 private final LinearGradient mGradientStops; 197 private final Matrix mMatrix; 198 ShadersView(Context c)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); 206 mGradientStops = new LinearGradient(0, 0, 0, 1, 207 new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }, null, Shader.TileMode.CLAMP); 208 209 mMatrix = new Matrix(); 210 211 mPaint = new Paint(); 212 } 213 214 @Override onDraw(Canvas canvas)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 226 mMatrix.setScale(1, mDrawWidth); 227 mMatrix.postRotate(90); 228 mMatrix.postTranslate(right, top); 229 mGradient.setLocalMatrix(mMatrix); 230 mPaint.setShader(mGradient); 231 canvas.drawRect(right - mDrawWidth, top, right, top + mDrawHeight, mPaint); 232 233 top += 40.0f + mDrawHeight; 234 bottom += 40.0f + mDrawHeight; 235 236 mMatrix.setScale(1, mDrawHeight); 237 mMatrix.postTranslate(left, top); 238 mGradient.setLocalMatrix(mMatrix); 239 mPaint.setShader(mGradient); 240 canvas.drawRect(left, top, right, top + mDrawHeight, mPaint); 241 242 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); 251 mPaint.setShader(mGradient); 252 canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint); 253 254 top += 40.0f + mDrawHeight; 255 bottom += 40.0f + mDrawHeight; 256 257 mMatrix.setScale(1, mDrawWidth); 258 mMatrix.postRotate(-90); 259 mMatrix.postTranslate(left, top); 260 mGradient.setLocalMatrix(mMatrix); 261 mPaint.setShader(mGradient); 262 canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint); 263 264 right = left + mDrawWidth; 265 left = 40.0f; 266 top = bottom + 20.0f; 267 bottom = top + 50.0f; 268 269 mMatrix.setScale(1, mDrawWidth); 270 mMatrix.postRotate(90); 271 mMatrix.postTranslate(right, top); 272 mGradientStops.setLocalMatrix(mMatrix); 273 mPaint.setShader(mGradientStops); 274 canvas.drawRect(left, top, right, bottom, mPaint); 275 276 canvas.restore(); 277 } 278 } 279 } 280