GradientsActivity.java revision 14830948d02f768c41b97b7a8d15e1b3cab78267
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.LinearGradient; 26import android.graphics.RadialGradient; 27import android.graphics.Matrix; 28import android.graphics.Paint; 29import android.graphics.Shader; 30import android.graphics.SweepGradient; 31import android.os.Bundle; 32import android.view.Gravity; 33import android.view.View; 34import android.widget.FrameLayout; 35import android.widget.SeekBar; 36 37@SuppressWarnings({"UnusedDeclaration"}) 38public class GradientsActivity extends Activity { 39 @Override 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 @Override 55 public void onStopTrackingTouch(SeekBar seekBar) { 56 } 57 58 @Override 59 public void onStartTrackingTouch(SeekBar seekBar) { 60 } 61 62 @Override 63 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 64 gradientView.setRotationY((float) progress); 65 radialGradientView.setRotationX((float) progress); 66 sweepGradientView.setRotationY((float) progress); 67 bitmapView.setRotationX((float) progress); 68 } 69 }); 70 71 layout.addView(shadersView); 72 layout.addView(gradientView, new FrameLayout.LayoutParams( 73 200, 200, Gravity.CENTER)); 74 75 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER); 76 lp.setMargins(220, 0, 0, 0); 77 layout.addView(radialGradientView, lp); 78 79 lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER); 80 lp.setMargins(440, 0, 0, 0); 81 layout.addView(sweepGradientView, lp); 82 83 lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER); 84 lp.setMargins(220, -220, 0, 0); 85 layout.addView(bitmapView, lp); 86 87 layout.addView(rotateView, new FrameLayout.LayoutParams( 88 300, FrameLayout.LayoutParams.WRAP_CONTENT, 89 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)); 90 91 setContentView(layout); 92 } 93 94 static class BitmapView extends View { 95 private final Paint mPaint; 96 97 BitmapView(Context c) { 98 super(c); 99 100 Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1); 101 BitmapShader shader = new BitmapShader(texture, Shader.TileMode.REPEAT, 102 Shader.TileMode.REPEAT); 103 mPaint = new Paint(); 104 mPaint.setShader(shader); 105 } 106 107 @Override 108 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 109 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 110 setMeasuredDimension(200, 200); 111 } 112 113 @Override 114 protected void onDraw(Canvas canvas) { 115 super.onDraw(canvas); 116 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 117 } 118 } 119 120 static class GradientView extends View { 121 private final Paint mPaint; 122 123 GradientView(Context c) { 124 super(c); 125 126 LinearGradient gradient = new LinearGradient(0, 0, 200, 0, 0xFF000000, 0, 127 Shader.TileMode.CLAMP); 128 mPaint = new Paint(); 129 mPaint.setShader(gradient); 130 } 131 132 @Override 133 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 134 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 135 setMeasuredDimension(200, 200); 136 } 137 138 @Override 139 protected void onDraw(Canvas canvas) { 140 super.onDraw(canvas); 141 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 142 } 143 } 144 145 static class RadialGradientView extends View { 146 private final Paint mPaint; 147 148 RadialGradientView(Context c) { 149 super(c); 150 151 RadialGradient gradient = new RadialGradient(0.0f, 0.0f, 100.0f, 0xff000000, 0xffffffff, 152 Shader.TileMode.MIRROR); 153 mPaint = new Paint(); 154 mPaint.setShader(gradient); 155 } 156 157 @Override 158 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 159 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 160 setMeasuredDimension(200, 200); 161 } 162 163 @Override 164 protected void onDraw(Canvas canvas) { 165 super.onDraw(canvas); 166 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 167 } 168 } 169 170 static class SweepGradientView extends View { 171 private final Paint mPaint; 172 173 SweepGradientView(Context c) { 174 super(c); 175 176 SweepGradient gradient = new SweepGradient(100.0f, 100.0f, 0xff000000, 0xffffffff); 177 mPaint = new Paint(); 178 mPaint.setShader(gradient); 179 } 180 181 @Override 182 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 183 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 184 setMeasuredDimension(200, 200); 185 } 186 187 @Override 188 protected void onDraw(Canvas canvas) { 189 super.onDraw(canvas); 190 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 191 } 192 } 193 194 static class ShadersView extends View { 195 private final Paint mPaint; 196 private final float mDrawWidth; 197 private final float mDrawHeight; 198 private final LinearGradient mGradient; 199 private final Matrix mMatrix; 200 201 ShadersView(Context c) { 202 super(c); 203 204 mDrawWidth = 200; 205 mDrawHeight = 200; 206 207 mGradient = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP); 208 mMatrix = new Matrix(); 209 210 mPaint = new Paint(); 211 } 212 213 @Override 214 protected void onDraw(Canvas canvas) { 215 super.onDraw(canvas); 216 canvas.drawRGB(255, 255, 255); 217 218 // Gradients 219 canvas.save(); 220 float top = 40.0f; 221 float right = 40.0f + mDrawWidth; 222 float left = 40.0f; 223 float bottom = 40.0f + mDrawHeight; 224 225 mPaint.setShader(mGradient); 226 227 mMatrix.setScale(1, mDrawWidth); 228 mMatrix.postRotate(90); 229 mMatrix.postTranslate(right, top); 230 mGradient.setLocalMatrix(mMatrix); 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 canvas.drawRect(left, top, right, top + mDrawHeight, mPaint); 240 241 left += 40.0f + mDrawWidth; 242 right += 40.0f + mDrawWidth; 243 top -= 40.0f + mDrawHeight; 244 bottom -= 40.0f + mDrawHeight; 245 246 mMatrix.setScale(1, mDrawHeight); 247 mMatrix.postRotate(180); 248 mMatrix.postTranslate(left, bottom); 249 mGradient.setLocalMatrix(mMatrix); 250 canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint); 251 252 top += 40.0f + mDrawHeight; 253 bottom += 40.0f + mDrawHeight; 254 255 mMatrix.setScale(1, mDrawWidth); 256 mMatrix.postRotate(-90); 257 mMatrix.postTranslate(left, top); 258 mGradient.setLocalMatrix(mMatrix); 259 canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint); 260 261 canvas.restore(); 262 } 263 } 264} 265