GradientsActivity.java revision e3095e0c1e2a4a4f34f741aa386eae56536ca5aa
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.Canvas; 22import android.graphics.LinearGradient; 23import android.graphics.Matrix; 24import android.graphics.Paint; 25import android.graphics.Shader; 26import android.os.Bundle; 27import android.view.Gravity; 28import android.view.View; 29import android.widget.FrameLayout; 30import android.widget.SeekBar; 31 32@SuppressWarnings({"UnusedDeclaration"}) 33public class GradientsActivity extends Activity { 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 final FrameLayout layout = new FrameLayout(this); 39 final ShadersView shadersView = new ShadersView(this); 40 final GradientView gradientView = new GradientView(this); 41 final SeekBar rotateView = new SeekBar(this); 42 rotateView.setMax(360); 43 rotateView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 44 @Override 45 public void onStopTrackingTouch(SeekBar seekBar) { 46 } 47 48 @Override 49 public void onStartTrackingTouch(SeekBar seekBar) { 50 } 51 52 @Override 53 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 54 gradientView.setRotationY((float)progress); 55 } 56 }); 57 58 layout.addView(shadersView); 59 layout.addView(gradientView, new FrameLayout.LayoutParams( 60 200, 200, Gravity.CENTER)); 61 layout.addView(rotateView, new FrameLayout.LayoutParams( 62 300, FrameLayout.LayoutParams.WRAP_CONTENT, 63 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)); 64 65 setContentView(layout); 66 } 67 68 static class GradientView extends View { 69 private final Paint mPaint; 70 71 GradientView(Context c) { 72 super(c); 73 74 LinearGradient gradient = new LinearGradient(0, 0, 200, 0, 0xFF000000, 0, 75 Shader.TileMode.CLAMP); 76 mPaint = new Paint(); 77 mPaint.setShader(gradient); 78 } 79 80 @Override 81 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 82 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 83 setMeasuredDimension(200, 200); 84 } 85 86 @Override 87 protected void onDraw(Canvas canvas) { 88 super.onDraw(canvas); 89 canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); 90 } 91 } 92 93 static class ShadersView extends View { 94 private final Paint mPaint; 95 private final float mDrawWidth; 96 private final float mDrawHeight; 97 private final LinearGradient mGradient; 98 private final Matrix mMatrix; 99 100 ShadersView(Context c) { 101 super(c); 102 103 mDrawWidth = 200; 104 mDrawHeight = 200; 105 106 mGradient = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP); 107 mMatrix = new Matrix(); 108 109 mPaint = new Paint(); 110 } 111 112 @Override 113 protected void onDraw(Canvas canvas) { 114 super.onDraw(canvas); 115 canvas.drawRGB(255, 255, 255); 116 117 // Gradients 118 canvas.save(); 119 float top = 40.0f; 120 float right = 40.0f + mDrawWidth; 121 float left = 40.0f; 122 float bottom = 40.0f + mDrawHeight; 123 124 mPaint.setShader(mGradient); 125 126 mMatrix.setScale(1, mDrawWidth); 127 mMatrix.postRotate(90); 128 mMatrix.postTranslate(right, top); 129 mGradient.setLocalMatrix(mMatrix); 130 canvas.drawRect(right - mDrawWidth, top, right, top + mDrawHeight, mPaint); 131 132 top += 40.0f + mDrawHeight; 133 bottom += 40.0f + mDrawHeight; 134 135 mMatrix.setScale(1, mDrawHeight); 136 mMatrix.postTranslate(left, top); 137 mGradient.setLocalMatrix(mMatrix); 138 canvas.drawRect(left, top, right, top + mDrawHeight, mPaint); 139 140 left += 40.0f + mDrawWidth; 141 right += 40.0f + mDrawWidth; 142 top -= 40.0f + mDrawHeight; 143 bottom -= 40.0f + mDrawHeight; 144 145 mMatrix.setScale(1, mDrawHeight); 146 mMatrix.postRotate(180); 147 mMatrix.postTranslate(left, bottom); 148 mGradient.setLocalMatrix(mMatrix); 149 canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint); 150 151 top += 40.0f + mDrawHeight; 152 bottom += 40.0f + mDrawHeight; 153 154 mMatrix.setScale(1, mDrawWidth); 155 mMatrix.postRotate(-90); 156 mMatrix.postTranslate(left, top); 157 mGradient.setLocalMatrix(mMatrix); 158 canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint); 159 160 canvas.restore(); 161 } 162 } 163} 164