1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.home;
import android.support.v4.content.ContextCompat;
import org.mozilla.gecko.R;
import org.mozilla.gecko.ThumbnailHelper;
import org.mozilla.gecko.widget.CropImageView;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
/**
* A width constrained ImageView to show thumbnails of top and pinned sites.
*/
public class TopSitesThumbnailView extends CropImageView {
private static final String LOGTAG = "GeckoTopSitesThumbnailView";
// 27.34% opacity filter for the dominant color.
private static final int COLOR_FILTER = 0x46FFFFFF;
// Default filter color for "Add a bookmark" views.
private final int mDefaultColor = ContextCompat.getColor(getContext(), R.color.top_site_default);
// Stroke width for the border.
private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2;
// Paint for drawing the border.
private final Paint mBorderPaint;
public TopSitesThumbnailView(Context context) {
this(context, null);
// A border will be drawn if needed.
setWillNotDraw(false);
}
public TopSitesThumbnailView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.topSitesThumbnailViewStyle);
}
public TopSitesThumbnailView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Initialize the border paint.
final Resources res = getResources();
mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBorderPaint.setColor(ContextCompat.getColor(context, R.color.top_site_border));
mBorderPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected float getAspectRatio() {
return ThumbnailHelper.TOP_SITES_THUMBNAIL_ASPECT_RATIO;
}
/**
* {@inheritDoc}
*/
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getBackground() == null) {
mBorderPaint.setStrokeWidth(mStrokeWidth);
canvas.drawRect(0, 0, getWidth(), getHeight(), mBorderPaint);
}
}
/**
* Sets the background color with a filter to reduce the color opacity.
*
* @param color the color filter to apply over the drawable.
*/
public void setBackgroundColorWithOpacityFilter(int color) {
setBackgroundColor(color & COLOR_FILTER);
}
/**
* Sets the background to a Drawable by applying the specified color as a filter.
*
* @param color the color filter to apply over the drawable.
*/
@Override
public void setBackgroundColor(int color) {
if (color == 0) {
color = mDefaultColor;
}
Drawable drawable = getResources().getDrawable(R.drawable.top_sites_thumbnail_bg);
drawable.setColorFilter(color, Mode.SRC_ATOP);
setBackgroundDrawable(drawable);
}
}
|