Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 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 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 17 | package com.android.test.hwui; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 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 | 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 | |
| 55 | ShadersView(Context c) { |
| 56 | super(c); |
| 57 | |
| 58 | Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1); |
| 59 | mTexWidth = texture.getWidth(); |
| 60 | mTexHeight = texture.getHeight(); |
| 61 | mDrawWidth = mTexWidth * 2.2f; |
| 62 | mDrawHeight = mTexHeight * 1.2f; |
| 63 | |
| 64 | mRepeatShader = new BitmapShader(texture, Shader.TileMode.REPEAT, |
| 65 | Shader.TileMode.REPEAT); |
| 66 | |
| 67 | mTranslatedShader = new BitmapShader(texture, Shader.TileMode.REPEAT, |
| 68 | Shader.TileMode.REPEAT); |
| 69 | Matrix m1 = new Matrix(); |
| 70 | m1.setTranslate(mTexWidth / 2.0f, mTexHeight / 2.0f); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 71 | m1.postRotate(45, 0, 0); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 72 | mTranslatedShader.setLocalMatrix(m1); |
| 73 | |
| 74 | mScaledShader = new BitmapShader(texture, Shader.TileMode.MIRROR, |
| 75 | Shader.TileMode.MIRROR); |
| 76 | Matrix m2 = new Matrix(); |
| 77 | m2.setScale(0.5f, 0.5f); |
| 78 | mScaledShader.setLocalMatrix(m2); |
| 79 | |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 80 | mHorGradient = new LinearGradient(0.0f, 0.0f, 1.0f, 0.0f, |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 81 | Color.RED, Color.GREEN, Shader.TileMode.CLAMP); |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 82 | Matrix m3 = new Matrix(); |
| 83 | m3.setScale(mDrawHeight, 1.0f); |
| 84 | m3.postRotate(-90.0f); |
| 85 | m3.postTranslate(0.0f, mDrawHeight); |
| 86 | mHorGradient.setLocalMatrix(m3); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 87 | |
| 88 | mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 1.5f, mDrawHeight, |
| 89 | Color.BLUE, Color.MAGENTA, Shader.TileMode.CLAMP); |
| 90 | |
| 91 | mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f, |
| 92 | Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR); |
| 93 | |
| 94 | mPaint = new Paint(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void onDraw(Canvas canvas) { |
| 99 | super.onDraw(canvas); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 100 | //canvas.drawRGB(255, 255, 255); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 101 | |
| 102 | // Bitmap shaders |
| 103 | canvas.save(); |
| 104 | canvas.translate(40.0f, 40.0f); |
| 105 | |
| 106 | mPaint.setShader(mRepeatShader); |
| 107 | canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); |
| 108 | |
| 109 | canvas.translate(0.0f, 40.0f + mDrawHeight); |
| 110 | mPaint.setShader(mTranslatedShader); |
| 111 | canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); |
| 112 | |
| 113 | canvas.translate(0.0f, 40.0f + mDrawHeight); |
| 114 | mPaint.setShader(mScaledShader); |
| 115 | canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); |
| 116 | |
| 117 | canvas.restore(); |
| 118 | |
| 119 | // Gradients |
| 120 | canvas.save(); |
| 121 | canvas.translate(40.0f + mDrawWidth + 40.0f, 40.0f); |
| 122 | |
| 123 | mPaint.setShader(mHorGradient); |
| 124 | canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); |
| 125 | |
| 126 | canvas.translate(0.0f, 40.0f + mDrawHeight); |
| 127 | mPaint.setShader(mDiagGradient); |
| 128 | canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); |
| 129 | |
| 130 | canvas.translate(0.0f, 40.0f + mDrawHeight); |
| 131 | mPaint.setShader(mVertGradient); |
| 132 | canvas.drawRect(0.0f, 0.0f, mDrawWidth, mDrawHeight, mPaint); |
| 133 | |
| 134 | canvas.restore(); |
| 135 | } |
| 136 | } |
| 137 | } |