OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.widget; |
| 6 |
| 7 import android.graphics.Bitmap; |
| 8 import android.graphics.BitmapShader; |
| 9 import android.graphics.Canvas; |
| 10 import android.graphics.Color; |
| 11 import android.graphics.ColorFilter; |
| 12 import android.graphics.Matrix; |
| 13 import android.graphics.Paint; |
| 14 import android.graphics.PixelFormat; |
| 15 import android.graphics.PorterDuff; |
| 16 import android.graphics.PorterDuffColorFilter; |
| 17 import android.graphics.Rect; |
| 18 import android.graphics.RectF; |
| 19 import android.graphics.Shader; |
| 20 import android.graphics.drawable.Drawable; |
| 21 |
| 22 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkSalientImag
eView.BaseSalientDrawable; |
| 23 |
| 24 /** |
| 25 * Base class for drawables that are to control what shape they take. |
| 26 */ |
| 27 public abstract class CustomShapeDrawable extends Drawable { |
| 28 |
| 29 /** |
| 30 * Circular drawable that makes a round mask on top of bitmap. |
| 31 */ |
| 32 public static class CircularDrawable extends CustomShapeDrawable { |
| 33 /** |
| 34 * Create a drawable based on the bitmap of the salient image. |
| 35 */ |
| 36 public CircularDrawable(Bitmap bitmap) { |
| 37 super(bitmap); |
| 38 } |
| 39 |
| 40 /** |
| 41 * Create a drawable based on the pure color. |
| 42 */ |
| 43 public CircularDrawable(int color) { |
| 44 super(color); |
| 45 } |
| 46 |
| 47 @Override |
| 48 protected void onBoundsChange(Rect bounds) { |
| 49 mRect.set(0, 0, bounds.width(), bounds.height()); |
| 50 super.onBoundsChange(bounds); |
| 51 } |
| 52 |
| 53 @Override |
| 54 public void draw(Canvas canvas) { |
| 55 canvas.drawCircle(mRect.centerX(), mRect.centerY(), mRect.height() *
0.5f, mPaint); |
| 56 } |
| 57 } |
| 58 |
| 59 /** |
| 60 * A {@link CircularDrawable} with dark background. |
| 61 */ |
| 62 public static class DarkBackgroundCircularDrawable extends CircularDrawable
{ |
| 63 private static final int FOLDER_CIRCLE_BACKGROUND_COLOR = Color.parseCol
or("#9E9E9E"); |
| 64 private Paint mBackgroundPaint = new Paint(); |
| 65 |
| 66 public DarkBackgroundCircularDrawable(Bitmap bitmap) { |
| 67 super(bitmap); |
| 68 shouldScale(false); |
| 69 mBackgroundPaint.setColor(FOLDER_CIRCLE_BACKGROUND_COLOR); |
| 70 mBackgroundPaint.setAntiAlias(true); |
| 71 mPaint.setColorFilter(new PorterDuffColorFilter(Color.WHITE, PorterD
uff.Mode.SRC_IN)); |
| 72 } |
| 73 |
| 74 @Override |
| 75 public void draw(Canvas canvas) { |
| 76 canvas.drawCircle(mRect.centerX(), mRect.centerY(), mRect.height() *
0.5f, |
| 77 mBackgroundPaint); |
| 78 super.draw(canvas); |
| 79 } |
| 80 } |
| 81 |
| 82 /** |
| 83 * Drawable that has rounded top corners, with image or pure color as conten
t. |
| 84 */ |
| 85 public static class TopRoundedCornerDrawable extends BaseSalientDrawable { |
| 86 private final float mRadius; |
| 87 |
| 88 /** |
| 89 * Create a drawable based on bitmap of salient image as well as radius. |
| 90 */ |
| 91 public TopRoundedCornerDrawable(Bitmap bitmap, float radius) { |
| 92 super(bitmap); |
| 93 mRadius = radius; |
| 94 } |
| 95 |
| 96 /** |
| 97 * Create a drawable based on the pure color as well as radius. |
| 98 */ |
| 99 public TopRoundedCornerDrawable(int color, float radius) { |
| 100 super(color); |
| 101 mRadius = radius; |
| 102 } |
| 103 |
| 104 @Override |
| 105 protected void onBoundsChange(Rect bounds) { |
| 106 // We avoid drawing bottom corners just by making it taller by the r
adius. |
| 107 mRect.set(0, 0, bounds.width(), bounds.height() + mRadius); |
| 108 super.onBoundsChange(bounds); |
| 109 } |
| 110 |
| 111 @Override |
| 112 public void draw(Canvas canvas) { |
| 113 canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint); |
| 114 } |
| 115 } |
| 116 |
| 117 protected RectF mRect = new RectF(); |
| 118 protected Paint mPaint = new Paint(); |
| 119 private int mBitmapWidth; |
| 120 private int mBitmapHeight; |
| 121 private boolean mShouldScale = true; |
| 122 |
| 123 @Override |
| 124 protected void onBoundsChange(Rect bounds) { |
| 125 // Subclasses must override this function. It should first set up correc
t boundary of |
| 126 // mRect and then call super.onBoundsChange(). |
| 127 if (mBitmapWidth > 0 && mBitmapHeight > 0) { |
| 128 BitmapShader shader = (BitmapShader) mPaint.getShader(); |
| 129 |
| 130 float scale = 1.0f; |
| 131 if (mShouldScale) { |
| 132 scale = Math.max((float) bounds.width() / mBitmapWidth, |
| 133 (float) bounds.height() / mBitmapHeight); |
| 134 } |
| 135 float dx = (bounds.width() - mBitmapWidth * scale) * 0.5f + bounds.l
eft; |
| 136 float dy = (bounds.height() - mBitmapHeight * scale) * 0.5f + bounds
.top; |
| 137 |
| 138 Matrix matrix = new Matrix(); |
| 139 matrix.setScale(scale, scale); |
| 140 matrix.postTranslate(dx, dy); |
| 141 shader.setLocalMatrix(matrix); |
| 142 } |
| 143 } |
| 144 |
| 145 protected CustomShapeDrawable(Bitmap bitmap) { |
| 146 mPaint.setAntiAlias(true); |
| 147 mPaint.setShader( |
| 148 new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.
CLAMP)); |
| 149 mBitmapWidth = bitmap.getWidth(); |
| 150 mBitmapHeight = bitmap.getHeight(); |
| 151 } |
| 152 |
| 153 protected CustomShapeDrawable(int color) { |
| 154 mPaint.setAntiAlias(true); |
| 155 mPaint.setColor(color); |
| 156 mBitmapWidth = 0; |
| 157 mBitmapHeight = 0; |
| 158 } |
| 159 |
| 160 /** |
| 161 * @param shouldScale True to let bitmap be scaled to match the size of |
| 162 * the drawable. False to let it maintain its original si
ze, |
| 163 * regardless of the size of the drawable. |
| 164 */ |
| 165 public void shouldScale(boolean shouldScale) { |
| 166 mShouldScale = shouldScale; |
| 167 } |
| 168 |
| 169 @Override |
| 170 public int getOpacity() { |
| 171 return PixelFormat.TRANSLUCENT; |
| 172 } |
| 173 |
| 174 @Override |
| 175 public void setAlpha(int alpha) { |
| 176 mPaint.setAlpha(alpha); |
| 177 } |
| 178 |
| 179 @Override |
| 180 public void setColorFilter(ColorFilter cf) { |
| 181 mPaint.setColorFilter(cf); |
| 182 } |
| 183 |
| 184 } |
OLD | NEW |