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