ShadersActivity.java revision d348b6eaa98e23cb38d90906df109aaa2d20ea7f
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.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 public 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 private Bitmap mTexture; 55 56 public ShadersView(Context c) { 57 super(c); 58 59 mTexture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1); 60 mTexWidth = mTexture.getWidth(); 61 mTexHeight = mTexture.getHeight(); 62 mDrawWidth = mTexWidth * 2.2f; 63 mDrawHeight = mTexHeight * 1.2f; 64 65 mRepeatShader = new BitmapShader(mTexture, Shader.TileMode.REPEAT, 66 Shader.TileMode.REPEAT); 67 68 mTranslatedShader = new BitmapShader(mTexture, Shader.TileMode.REPEAT, 69 Shader.TileMode.REPEAT); 70 Matrix m1 = new Matrix(); 71 m1.setTranslate(mTexWidth / 2.0f, mTexHeight / 2.0f); 72 m1.postRotate(45, 0, 0); 73 mTranslatedShader.setLocalMatrix(m1); 74 75 mScaledShader = new BitmapShader(mTexture, Shader.TileMode.MIRROR, 76 Shader.TileMode.MIRROR); 77 Matrix m2 = new Matrix(); 78 m2.setScale(0.5f, 0.5f); 79 mScaledShader.setLocalMatrix(m2); 80 81 mHorGradient = new LinearGradient(0.0f, 0.0f, 1.0f, 0.0f, 82 Color.RED, Color.GREEN, Shader.TileMode.CLAMP); 83 Matrix m3 = new Matrix(); 84 m3.setScale(mDrawHeight, 1.0f); 85 m3.postRotate(-90.0f); 86 m3.postTranslate(0.0f, mDrawHeight); 87 mHorGradient.setLocalMatrix(m3); 88 89 mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 1.5f, mDrawHeight, 90 Color.BLUE, Color.MAGENTA, Shader.TileMode.CLAMP); 91 92 mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f, 93 Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR); 94 95 mPaint = new Paint(); 96 } 97 98 @Override 99 protected void onDraw(Canvas canvas) { 100 super.onDraw(canvas); 101 //canvas.drawRGB(255, 255, 255); 102 canvas.drawBitmap(mTexture, 0.0f, 0.0f, null); 103 104 // Bitmap shaders 105 canvas.save(); 106 canvas.translate(40.0f, 40.0f); 107 108 mPaint.setShader(mRepeatShader); 109 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); 110 111 canvas.translate(0.0f, 40.0f + mDrawHeight); 112 mPaint.setShader(mTranslatedShader); 113 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); 114 115 canvas.translate(0.0f, 40.0f + mDrawHeight); 116 mPaint.setShader(mScaledShader); 117 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); 118 119 canvas.restore(); 120 121 // Gradients 122 canvas.save(); 123 canvas.translate(40.0f + mDrawWidth + 40.0f, 40.0f); 124 125 mPaint.setShader(mHorGradient); 126 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); 127 128 canvas.translate(0.0f, 40.0f + mDrawHeight); 129 mPaint.setShader(mDiagGradient); 130 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); 131 132 canvas.translate(0.0f, 40.0f + mDrawHeight); 133 mPaint.setShader(mVertGradient); 134 canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); 135 136 canvas.restore(); 137 } 138 } 139} 140