1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/TopSitesThumbnailView.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.mozilla.gecko.R; 1.12 +import org.mozilla.gecko.ThumbnailHelper; 1.13 + 1.14 +import android.content.Context; 1.15 +import android.graphics.Canvas; 1.16 +import android.graphics.Paint; 1.17 +import android.graphics.PorterDuff.Mode; 1.18 +import android.graphics.drawable.Drawable; 1.19 +import android.util.AttributeSet; 1.20 +import android.widget.ImageView; 1.21 + 1.22 +/** 1.23 + * A height constrained ImageView to show thumbnails of top and pinned sites. 1.24 + */ 1.25 +public class TopSitesThumbnailView extends ImageView { 1.26 + private static final String LOGTAG = "GeckoTopSitesThumbnailView"; 1.27 + 1.28 + // 27.34% opacity filter for the dominant color. 1.29 + private static final int COLOR_FILTER = 0x46FFFFFF; 1.30 + 1.31 + // Default filter color for "Add a bookmark" views. 1.32 + private static final int DEFAULT_COLOR = 0x46ECF0F3; 1.33 + 1.34 + // Stroke width for the border. 1.35 + private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2; 1.36 + 1.37 + // Paint for drawing the border. 1.38 + private static Paint sBorderPaint; 1.39 + 1.40 + // Initializing the static border paint. 1.41 + static { 1.42 + sBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 1.43 + sBorderPaint.setColor(0xFFCFD9E1); 1.44 + sBorderPaint.setStyle(Paint.Style.STROKE); 1.45 + } 1.46 + 1.47 + public TopSitesThumbnailView(Context context) { 1.48 + this(context, null); 1.49 + 1.50 + // A border will be drawn if needed. 1.51 + setWillNotDraw(false); 1.52 + } 1.53 + 1.54 + public TopSitesThumbnailView(Context context, AttributeSet attrs) { 1.55 + this(context, attrs, R.attr.topSitesThumbnailViewStyle); 1.56 + } 1.57 + 1.58 + public TopSitesThumbnailView(Context context, AttributeSet attrs, int defStyle) { 1.59 + super(context, attrs, defStyle); 1.60 + } 1.61 + 1.62 + /** 1.63 + * Measure the view to determine the measured width and height. 1.64 + * The height is constrained by the measured width. 1.65 + * 1.66 + * @param widthMeasureSpec horizontal space requirements as imposed by the parent. 1.67 + * @param heightMeasureSpec vertical space requirements as imposed by the parent, but ignored. 1.68 + */ 1.69 + @Override 1.70 + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 1.71 + // Default measuring. 1.72 + super.onMeasure(widthMeasureSpec, heightMeasureSpec); 1.73 + 1.74 + // Force the height based on the aspect ratio. 1.75 + final int width = getMeasuredWidth(); 1.76 + final int height = (int) (width * ThumbnailHelper.THUMBNAIL_ASPECT_RATIO); 1.77 + setMeasuredDimension(width, height); 1.78 + } 1.79 + 1.80 + /** 1.81 + * {@inheritDoc} 1.82 + */ 1.83 + @Override 1.84 + public void onDraw(Canvas canvas) { 1.85 + super.onDraw(canvas); 1.86 + 1.87 + if (getBackground() == null) { 1.88 + sBorderPaint.setStrokeWidth(mStrokeWidth); 1.89 + canvas.drawRect(0, 0, getWidth(), getHeight(), sBorderPaint); 1.90 + } 1.91 + } 1.92 + 1.93 + /** 1.94 + * Sets the background to a Drawable by applying the specified color as a filter. 1.95 + * 1.96 + * @param color the color filter to apply over the drawable. 1.97 + */ 1.98 + @Override 1.99 + public void setBackgroundColor(int color) { 1.100 + int colorFilter = color == 0 ? DEFAULT_COLOR : color & COLOR_FILTER; 1.101 + Drawable drawable = getResources().getDrawable(R.drawable.top_sites_thumbnail_bg); 1.102 + drawable.setColorFilter(colorFilter, Mode.SRC_ATOP); 1.103 + setBackgroundDrawable(drawable); 1.104 + } 1.105 +}