Bug 1265497 - FaviconView: New favicon background with rounded corners. r?ahunt
MozReview-Commit-ID: Fj1YRU2fZYF
--- a/mobile/android/base/java/org/mozilla/gecko/widget/FaviconView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/FaviconView.java
@@ -11,25 +11,30 @@ import org.mozilla.gecko.favicons.Favico
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
+import android.util.DisplayMetrics;
+import android.util.TypedValue;
import android.widget.ImageView;
/**
* Special version of ImageView for favicons.
* Displays solid colour background around Favicon to fill space not occupied by the icon. Colour
* selected is the dominant colour of the provided Favicon.
*/
public class FaviconView extends ImageView {
private static String DEFAULT_FAVICON_KEY = FaviconView.class.getSimpleName() + "DefaultFavicon";
+ // Default x/y-radius of the oval used to round the corners of the background (dp)
+ private static final int DEFAULT_CORNER_RADIUS_DP = 4;
+
private Bitmap mIconBitmap;
// Reference to the unscaled bitmap, if any, to prevent repeated assignments of the same bitmap
// to the view from causing repeated rescalings (Some of the callers do this)
private Bitmap mUnscaledBitmap;
// Key into the Favicon dominant colour cache. Should be the Favicon URL if the image displayed
// here is a Favicon managed by the caching system. If not, any appropriately unique-to-this-image
@@ -40,42 +45,33 @@ public class FaviconView extends ImageVi
private int mActualHeight;
// Flag indicating if the most recently assigned image is considered likely to need scaling.
private boolean mScalingExpected;
// Dominant color of the favicon.
private int mDominantColor;
- // Stroke width for the border.
- private static float sStrokeWidth;
-
- // Paint for drawing the stroke.
- private static final Paint sStrokePaint;
-
// Paint for drawing the background.
private static final Paint sBackgroundPaint;
- // Size of the stroke rectangle.
- private final RectF mStrokeRect;
-
// Size of the background rectangle.
private final RectF mBackgroundRect;
+ // The x/y-radius of the oval used to round the corners of the background (pixels)
+ private final float mBackgroundCornerRadius;
+
// Type of the border whose value is defined in attrs.xml .
private final boolean isDominantBorderEnabled;
// boolean switch for overriding scaletype, whose value is defined in attrs.xml .
private final boolean isOverrideScaleTypeEnabled;
// Initializing the static paints.
static {
- sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- sStrokePaint.setStyle(Paint.Style.STROKE);
-
sBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
sBackgroundPaint.setStyle(Paint.Style.FILL);
}
public FaviconView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FaviconView, 0, 0);
@@ -85,60 +81,49 @@ public class FaviconView extends ImageVi
} finally {
a.recycle();
}
if (isOverrideScaleTypeEnabled) {
setScaleType(ImageView.ScaleType.CENTER);
}
- mStrokeRect = new RectF();
- mBackgroundRect = new RectF();
+ final DisplayMetrics metrics = getResources().getDisplayMetrics();
- if (sStrokeWidth == 0) {
- sStrokeWidth = getResources().getDisplayMetrics().density;
- sStrokePaint.setStrokeWidth(sStrokeWidth);
- }
-
- mStrokeRect.left = mStrokeRect.top = sStrokeWidth;
- mBackgroundRect.left = mBackgroundRect.top = sStrokeWidth * 2.0f;
+ mBackgroundRect = new RectF(0, 0, 0, 0);
+ mBackgroundCornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_CORNER_RADIUS_DP, metrics);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// No point rechecking the image if there hasn't really been any change.
if (w == mActualWidth && h == mActualHeight) {
return;
}
mActualWidth = w;
mActualHeight = h;
- mStrokeRect.right = w - sStrokeWidth;
- mStrokeRect.bottom = h - sStrokeWidth;
- mBackgroundRect.right = mStrokeRect.right - sStrokeWidth;
- mBackgroundRect.bottom = mStrokeRect.bottom - sStrokeWidth;
+ mBackgroundRect.right = w;
+ mBackgroundRect.bottom = h;
formatImage();
}
@Override
public void onDraw(Canvas canvas) {
- super.onDraw(canvas);
+ if (isDominantBorderEnabled) {
+ sBackgroundPaint.setColor(mDominantColor & 0x7FFFFFFF);
- if (isDominantBorderEnabled) {
- // 27.5% transparent dominant color.
- sBackgroundPaint.setColor(mDominantColor & 0x46FFFFFF);
- canvas.drawRect(mStrokeRect, sBackgroundPaint);
+ canvas.drawRoundRect(mBackgroundRect, mBackgroundCornerRadius, mBackgroundCornerRadius, sBackgroundPaint);
+ }
- sStrokePaint.setColor(mDominantColor);
- canvas.drawRoundRect(mStrokeRect, sStrokeWidth, sStrokeWidth, sStrokePaint);
- }
+ super.onDraw(canvas);
}
/**
* Formats the image for display, if the prerequisite data are available. Upscales tiny Favicons to
* normal sized ones, replaces null bitmaps with the default Favicon, and fills all remaining space
* in this view with the coloured background.
*/
private void formatImage() {