1 /* 2 * Copyright (C) 2012 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.hwuicompare; 18 19 import com.android.test.hwuicompare.R; 20 21 import android.content.res.Resources; 22 import android.graphics.Bitmap; 23 import android.graphics.BitmapFactory; 24 import android.graphics.BitmapShader; 25 import android.graphics.Color; 26 import android.graphics.LinearGradient; 27 import android.graphics.Matrix; 28 import android.graphics.Shader; 29 30 public class ResourceModifiers { 31 public final BitmapShader mRepeatShader; 32 public final BitmapShader mTranslatedShader; 33 public final BitmapShader mScaledShader; 34 private final int mTexWidth; 35 private final int mTexHeight; 36 private final float mDrawWidth; 37 private final float mDrawHeight; 38 public final LinearGradient mHorGradient; 39 public final LinearGradient mDiagGradient; 40 public final LinearGradient mVertGradient; 41 public final Bitmap mBitmap; 42 private final Matrix mMtx1; 43 private final Matrix mMtx2; 44 private final Matrix mMtx3; 45 46 public final float[] mBitmapVertices; 47 public final int[] mBitmapColors; 48 49 private static ResourceModifiers sInstance = null; 50 public static ResourceModifiers instance() { return sInstance; } 51 public static void init(Resources resources) { 52 sInstance = new ResourceModifiers(resources); 53 } 54 55 public ResourceModifiers(Resources resources) { 56 mBitmap = BitmapFactory.decodeResource(resources, R.drawable.sunset1); 57 mTexWidth = mBitmap.getWidth(); 58 mTexHeight = mBitmap.getHeight(); 59 60 mDrawWidth = resources.getDimensionPixelSize(R.dimen.layer_width); 61 mDrawHeight = resources.getDimensionPixelSize(R.dimen.layer_height); 62 63 mRepeatShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, 64 Shader.TileMode.REPEAT); 65 66 mTranslatedShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, 67 Shader.TileMode.REPEAT); 68 mMtx1 = new Matrix(); 69 mMtx1.setTranslate(mTexWidth / 2.0f, mTexHeight / 2.0f); 70 mMtx1.postRotate(45, 0, 0); 71 mTranslatedShader.setLocalMatrix(mMtx1); 72 73 mScaledShader = new BitmapShader(mBitmap, Shader.TileMode.MIRROR, 74 Shader.TileMode.MIRROR); 75 mMtx2 = new Matrix(); 76 mMtx2.setScale(0.5f, 0.5f); 77 mScaledShader.setLocalMatrix(mMtx2); 78 79 mHorGradient = new LinearGradient(0.0f, 0.0f, 1.0f, 0.0f, 80 Color.RED, Color.GREEN, Shader.TileMode.CLAMP); 81 mMtx3 = new Matrix(); 82 mMtx3.setScale(mDrawHeight, 1.0f); 83 mMtx3.postRotate(-90.0f); 84 mMtx3.postTranslate(0.0f, mDrawHeight); 85 mHorGradient.setLocalMatrix(mMtx3); 86 87 mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 2.0f, mDrawHeight / 2.0f, 88 Color.BLUE, Color.RED, Shader.TileMode.CLAMP); 89 90 mVertGradient = new LinearGradient(0.0f, 0.0f, 0.0f, mDrawHeight / 2.0f, 91 Color.YELLOW, Color.MAGENTA, Shader.TileMode.MIRROR); 92 93 final float width = mBitmap.getWidth() / 8.0f; 94 final float height = mBitmap.getHeight() / 8.0f; 95 96 mBitmapVertices = new float[] { 97 0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f, 98 0.0f, height, width, height, width * 2, height, width * 4, height, 99 0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2, 100 0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4, 101 }; 102 103 mBitmapColors = new int[] { 104 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff0000, 105 0xff0000ff, 0xffff0000, 0xff00ff00, 0xff00ff00, 106 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00, 107 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00ff0000, 108 }; 109 } 110 111 } 112