mobile/android/base/home/TopSitesThumbnailView.java

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:dce8037d753b
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/. */
5
6 package org.mozilla.gecko.home;
7
8 import org.mozilla.gecko.R;
9 import org.mozilla.gecko.ThumbnailHelper;
10
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;
18
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";
24
25 // 27.34% opacity filter for the dominant color.
26 private static final int COLOR_FILTER = 0x46FFFFFF;
27
28 // Default filter color for "Add a bookmark" views.
29 private static final int DEFAULT_COLOR = 0x46ECF0F3;
30
31 // Stroke width for the border.
32 private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2;
33
34 // Paint for drawing the border.
35 private static Paint sBorderPaint;
36
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 }
43
44 public TopSitesThumbnailView(Context context) {
45 this(context, null);
46
47 // A border will be drawn if needed.
48 setWillNotDraw(false);
49 }
50
51 public TopSitesThumbnailView(Context context, AttributeSet attrs) {
52 this(context, attrs, R.attr.topSitesThumbnailViewStyle);
53 }
54
55 public TopSitesThumbnailView(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
57 }
58
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);
70
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 }
76
77 /**
78 * {@inheritDoc}
79 */
80 @Override
81 public void onDraw(Canvas canvas) {
82 super.onDraw(canvas);
83
84 if (getBackground() == null) {
85 sBorderPaint.setStrokeWidth(mStrokeWidth);
86 canvas.drawRect(0, 0, getWidth(), getHeight(), sBorderPaint);
87 }
88 }
89
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