Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko; |
michael@0 | 7 | |
michael@0 | 8 | import android.content.res.Resources; |
michael@0 | 9 | import android.graphics.Bitmap; |
michael@0 | 10 | import android.graphics.BitmapShader; |
michael@0 | 11 | import android.graphics.Canvas; |
michael@0 | 12 | import android.graphics.ColorFilter; |
michael@0 | 13 | import android.graphics.ComposeShader; |
michael@0 | 14 | import android.graphics.LinearGradient; |
michael@0 | 15 | import android.graphics.Paint; |
michael@0 | 16 | import android.graphics.PixelFormat; |
michael@0 | 17 | import android.graphics.PorterDuff; |
michael@0 | 18 | import android.graphics.PorterDuffColorFilter; |
michael@0 | 19 | import android.graphics.Rect; |
michael@0 | 20 | import android.graphics.Shader; |
michael@0 | 21 | import android.graphics.drawable.Drawable; |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * A special drawable used with lightweight themes. This draws a color |
michael@0 | 25 | * (with an optional color-filter) and a bitmap (with a linear gradient |
michael@0 | 26 | * to specify the alpha) in order. |
michael@0 | 27 | */ |
michael@0 | 28 | public class LightweightThemeDrawable extends Drawable { |
michael@0 | 29 | private Paint mPaint; |
michael@0 | 30 | private Paint mColorPaint; |
michael@0 | 31 | |
michael@0 | 32 | private Bitmap mBitmap; |
michael@0 | 33 | private Resources mResources; |
michael@0 | 34 | |
michael@0 | 35 | private int mStartColor; |
michael@0 | 36 | private int mEndColor; |
michael@0 | 37 | |
michael@0 | 38 | public LightweightThemeDrawable(Resources resources, Bitmap bitmap) { |
michael@0 | 39 | mBitmap = bitmap; |
michael@0 | 40 | mResources = resources; |
michael@0 | 41 | |
michael@0 | 42 | mPaint = new Paint(); |
michael@0 | 43 | mPaint.setAntiAlias(true); |
michael@0 | 44 | mPaint.setStrokeWidth(0.0f); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | @Override |
michael@0 | 48 | protected void onBoundsChange(Rect bounds) { |
michael@0 | 49 | super.onBoundsChange(bounds); |
michael@0 | 50 | initializeBitmapShader(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | @Override |
michael@0 | 54 | public void draw(Canvas canvas) { |
michael@0 | 55 | // Draw the colors, if available. |
michael@0 | 56 | if (mColorPaint != null) |
michael@0 | 57 | canvas.drawPaint(mColorPaint); |
michael@0 | 58 | |
michael@0 | 59 | // Draw the bitmap. |
michael@0 | 60 | canvas.drawPaint(mPaint); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | @Override |
michael@0 | 64 | public int getOpacity() { |
michael@0 | 65 | return PixelFormat.TRANSLUCENT; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | @Override |
michael@0 | 69 | public void setAlpha(int alpha) { |
michael@0 | 70 | // A StateListDrawable will reset the alpha value with 255. |
michael@0 | 71 | // We cannot use to be the bitmap alpha. |
michael@0 | 72 | mPaint.setAlpha(alpha); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | @Override |
michael@0 | 76 | public void setColorFilter(ColorFilter filter) { |
michael@0 | 77 | mPaint.setColorFilter(filter); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * Creates a paint that paint a particular color. |
michael@0 | 82 | * |
michael@0 | 83 | * @param color The color to be painted. |
michael@0 | 84 | */ |
michael@0 | 85 | public void setColor(int color) { |
michael@0 | 86 | mColorPaint = new Paint(mPaint); |
michael@0 | 87 | mColorPaint.setColor(color); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Creates a paint that paint a particular color, and a filter for the color. |
michael@0 | 92 | * |
michael@0 | 93 | * @param color The color to be painted. |
michael@0 | 94 | * @param filter The filter color to be applied using SRC_OVER mode. |
michael@0 | 95 | */ |
michael@0 | 96 | public void setColorWithFilter(int color, int filter) { |
michael@0 | 97 | mColorPaint = new Paint(mPaint); |
michael@0 | 98 | mColorPaint.setColor(color); |
michael@0 | 99 | mColorPaint.setColorFilter(new PorterDuffColorFilter(filter, PorterDuff.Mode.SRC_OVER)); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Set the alpha for the linear gradient used with the bitmap's shader. |
michael@0 | 104 | * |
michael@0 | 105 | * @param startAlpha The starting alpha (0..255) value to be applied to the LinearGradient. |
michael@0 | 106 | * @param startAlpha The ending alpha (0..255) value to be applied to the LinearGradient. |
michael@0 | 107 | */ |
michael@0 | 108 | public void setAlpha(int startAlpha, int endAlpha) { |
michael@0 | 109 | mStartColor = startAlpha << 24; |
michael@0 | 110 | mEndColor = endAlpha << 24; |
michael@0 | 111 | initializeBitmapShader(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | private void initializeBitmapShader() { |
michael@0 | 115 | // A bitmap-shader to draw the bitmap. |
michael@0 | 116 | // Clamp mode will repeat the last row of pixels. |
michael@0 | 117 | // Hence its better to have an endAlpha of 0 for the linear-gradient. |
michael@0 | 118 | BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
michael@0 | 119 | |
michael@0 | 120 | // A linear-gradient to specify the opacity of the bitmap. |
michael@0 | 121 | LinearGradient gradient = new LinearGradient(0, 0, 0, mBitmap.getHeight(), mStartColor, mEndColor, Shader.TileMode.CLAMP); |
michael@0 | 122 | |
michael@0 | 123 | // Make a combined shader -- a performance win. |
michael@0 | 124 | // The linear-gradient is the 'SRC' and the bitmap-shader is the 'DST'. |
michael@0 | 125 | // Drawing the DST in the SRC will provide the opacity. |
michael@0 | 126 | mPaint.setShader(new ComposeShader(bitmapShader, gradient, PorterDuff.Mode.DST_IN)); |
michael@0 | 127 | } |
michael@0 | 128 | } |