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.home; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.R; |
michael@0 | 9 | import org.mozilla.gecko.ThumbnailHelper; |
michael@0 | 10 | |
michael@0 | 11 | import android.content.Context; |
michael@0 | 12 | import android.graphics.Canvas; |
michael@0 | 13 | import android.graphics.Paint; |
michael@0 | 14 | import android.graphics.PorterDuff.Mode; |
michael@0 | 15 | import android.graphics.drawable.Drawable; |
michael@0 | 16 | import android.util.AttributeSet; |
michael@0 | 17 | import android.widget.ImageView; |
michael@0 | 18 | |
michael@0 | 19 | /** |
michael@0 | 20 | * A height constrained ImageView to show thumbnails of top and pinned sites. |
michael@0 | 21 | */ |
michael@0 | 22 | public class TopSitesThumbnailView extends ImageView { |
michael@0 | 23 | private static final String LOGTAG = "GeckoTopSitesThumbnailView"; |
michael@0 | 24 | |
michael@0 | 25 | // 27.34% opacity filter for the dominant color. |
michael@0 | 26 | private static final int COLOR_FILTER = 0x46FFFFFF; |
michael@0 | 27 | |
michael@0 | 28 | // Default filter color for "Add a bookmark" views. |
michael@0 | 29 | private static final int DEFAULT_COLOR = 0x46ECF0F3; |
michael@0 | 30 | |
michael@0 | 31 | // Stroke width for the border. |
michael@0 | 32 | private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2; |
michael@0 | 33 | |
michael@0 | 34 | // Paint for drawing the border. |
michael@0 | 35 | private static Paint sBorderPaint; |
michael@0 | 36 | |
michael@0 | 37 | // Initializing the static border paint. |
michael@0 | 38 | static { |
michael@0 | 39 | sBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
michael@0 | 40 | sBorderPaint.setColor(0xFFCFD9E1); |
michael@0 | 41 | sBorderPaint.setStyle(Paint.Style.STROKE); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | public TopSitesThumbnailView(Context context) { |
michael@0 | 45 | this(context, null); |
michael@0 | 46 | |
michael@0 | 47 | // A border will be drawn if needed. |
michael@0 | 48 | setWillNotDraw(false); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | public TopSitesThumbnailView(Context context, AttributeSet attrs) { |
michael@0 | 52 | this(context, attrs, R.attr.topSitesThumbnailViewStyle); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | public TopSitesThumbnailView(Context context, AttributeSet attrs, int defStyle) { |
michael@0 | 56 | super(context, attrs, defStyle); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * Measure the view to determine the measured width and height. |
michael@0 | 61 | * The height is constrained by the measured width. |
michael@0 | 62 | * |
michael@0 | 63 | * @param widthMeasureSpec horizontal space requirements as imposed by the parent. |
michael@0 | 64 | * @param heightMeasureSpec vertical space requirements as imposed by the parent, but ignored. |
michael@0 | 65 | */ |
michael@0 | 66 | @Override |
michael@0 | 67 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
michael@0 | 68 | // Default measuring. |
michael@0 | 69 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
michael@0 | 70 | |
michael@0 | 71 | // Force the height based on the aspect ratio. |
michael@0 | 72 | final int width = getMeasuredWidth(); |
michael@0 | 73 | final int height = (int) (width * ThumbnailHelper.THUMBNAIL_ASPECT_RATIO); |
michael@0 | 74 | setMeasuredDimension(width, height); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * {@inheritDoc} |
michael@0 | 79 | */ |
michael@0 | 80 | @Override |
michael@0 | 81 | public void onDraw(Canvas canvas) { |
michael@0 | 82 | super.onDraw(canvas); |
michael@0 | 83 | |
michael@0 | 84 | if (getBackground() == null) { |
michael@0 | 85 | sBorderPaint.setStrokeWidth(mStrokeWidth); |
michael@0 | 86 | canvas.drawRect(0, 0, getWidth(), getHeight(), sBorderPaint); |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Sets the background to a Drawable by applying the specified color as a filter. |
michael@0 | 92 | * |
michael@0 | 93 | * @param color the color filter to apply over the drawable. |
michael@0 | 94 | */ |
michael@0 | 95 | @Override |
michael@0 | 96 | public void setBackgroundColor(int color) { |
michael@0 | 97 | int colorFilter = color == 0 ? DEFAULT_COLOR : color & COLOR_FILTER; |
michael@0 | 98 | Drawable drawable = getResources().getDrawable(R.drawable.top_sites_thumbnail_bg); |
michael@0 | 99 | drawable.setColorFilter(colorFilter, Mode.SRC_ATOP); |
michael@0 | 100 | setBackgroundDrawable(drawable); |
michael@0 | 101 | } |
michael@0 | 102 | } |