mobile/android/base/home/TopSitesThumbnailView.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial