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