michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.ThumbnailHelper; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.Canvas; michael@0: import android.graphics.Paint; michael@0: import android.graphics.PorterDuff.Mode; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.AttributeSet; michael@0: import android.widget.ImageView; michael@0: michael@0: /** michael@0: * A height constrained ImageView to show thumbnails of top and pinned sites. michael@0: */ michael@0: public class TopSitesThumbnailView extends ImageView { michael@0: private static final String LOGTAG = "GeckoTopSitesThumbnailView"; michael@0: michael@0: // 27.34% opacity filter for the dominant color. michael@0: private static final int COLOR_FILTER = 0x46FFFFFF; michael@0: michael@0: // Default filter color for "Add a bookmark" views. michael@0: private static final int DEFAULT_COLOR = 0x46ECF0F3; michael@0: michael@0: // Stroke width for the border. michael@0: private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2; michael@0: michael@0: // Paint for drawing the border. michael@0: private static Paint sBorderPaint; michael@0: michael@0: // Initializing the static border paint. michael@0: static { michael@0: sBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); michael@0: sBorderPaint.setColor(0xFFCFD9E1); michael@0: sBorderPaint.setStyle(Paint.Style.STROKE); michael@0: } michael@0: michael@0: public TopSitesThumbnailView(Context context) { michael@0: this(context, null); michael@0: michael@0: // A border will be drawn if needed. michael@0: setWillNotDraw(false); michael@0: } michael@0: michael@0: public TopSitesThumbnailView(Context context, AttributeSet attrs) { michael@0: this(context, attrs, R.attr.topSitesThumbnailViewStyle); michael@0: } michael@0: michael@0: public TopSitesThumbnailView(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: } michael@0: michael@0: /** michael@0: * Measure the view to determine the measured width and height. michael@0: * The height is constrained by the measured width. michael@0: * michael@0: * @param widthMeasureSpec horizontal space requirements as imposed by the parent. michael@0: * @param heightMeasureSpec vertical space requirements as imposed by the parent, but ignored. michael@0: */ michael@0: @Override michael@0: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { michael@0: // Default measuring. michael@0: super.onMeasure(widthMeasureSpec, heightMeasureSpec); michael@0: michael@0: // Force the height based on the aspect ratio. michael@0: final int width = getMeasuredWidth(); michael@0: final int height = (int) (width * ThumbnailHelper.THUMBNAIL_ASPECT_RATIO); michael@0: setMeasuredDimension(width, height); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: @Override michael@0: public void onDraw(Canvas canvas) { michael@0: super.onDraw(canvas); michael@0: michael@0: if (getBackground() == null) { michael@0: sBorderPaint.setStrokeWidth(mStrokeWidth); michael@0: canvas.drawRect(0, 0, getWidth(), getHeight(), sBorderPaint); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Sets the background to a Drawable by applying the specified color as a filter. michael@0: * michael@0: * @param color the color filter to apply over the drawable. michael@0: */ michael@0: @Override michael@0: public void setBackgroundColor(int color) { michael@0: int colorFilter = color == 0 ? DEFAULT_COLOR : color & COLOR_FILTER; michael@0: Drawable drawable = getResources().getDrawable(R.drawable.top_sites_thumbnail_bg); michael@0: drawable.setColorFilter(colorFilter, Mode.SRC_ATOP); michael@0: setBackgroundDrawable(drawable); michael@0: } michael@0: }