Bug 933459: Reduce drawables created for FaviconsView. [r=mfinkle]

This commit is contained in:
Sriram Ramasubramanian 2013-11-01 13:18:29 -07:00
Родитель 46a0db7e37
Коммит 72183ef896
3 изменённых файлов: 73 добавлений и 49 удалений

Просмотреть файл

@ -777,7 +777,6 @@ ANDROID_RESFILES += [
'resources/drawable/bookmark_folder.xml',
'resources/drawable/divider_horizontal.xml',
'resources/drawable/divider_vertical.xml',
'resources/drawable/favicon_bg.xml',
'resources/drawable/handle_end_level.xml',
'resources/drawable/handle_start_level.xml',
'resources/drawable/home_banner.xml',

Просмотреть файл

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/. -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/favicon_bg_radius"/>
<size android:height="@dimen/favicon_bg"/>
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#DDDDDD"/>
</shape>

Просмотреть файл

@ -5,14 +5,14 @@
package org.mozilla.gecko.widget;
import org.mozilla.gecko.favicons.Favicons;
import org.mozilla.gecko.R;
import org.mozilla.gecko.favicons.Favicons;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
@ -38,24 +38,81 @@ public class FaviconView extends ImageView {
// 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 Paint sStrokePaint;
// Paint for drawing the background.
private static Paint sBackgroundPaint;
// Size of the stroke rectangle.
private final RectF mStrokeRect;
// Size of the background rectangle.
private final RectF mBackgroundRect;
// 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);
setScaleType(ImageView.ScaleType.CENTER);
mStrokeRect = new RectF();
mBackgroundRect = new RectF();
if (sStrokeWidth == 0) {
sStrokeWidth = getResources().getDisplayMetrics().density;
sStrokePaint.setStrokeWidth(sStrokeWidth);
}
mStrokeRect.left = mStrokeRect.top = sStrokeWidth;
mBackgroundRect.left = mBackgroundRect.top = sStrokeWidth * 2.0f;
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
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 (xNew == mActualHeight && yNew == mActualWidth) {
if (w == mActualWidth && h == mActualHeight) {
return;
}
mActualWidth = xNew;
mActualHeight = yNew;
mActualWidth = w;
mActualHeight = h;
mStrokeRect.right = w - sStrokeWidth;
mStrokeRect.bottom = h - sStrokeWidth;
mBackgroundRect.right = mStrokeRect.right - sStrokeWidth;
mBackgroundRect.bottom = mStrokeRect.bottom - sStrokeWidth;
formatImage();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 27.5% transparent dominant color.
sBackgroundPaint.setColor(mDominantColor & 0x46FFFFFF);
canvas.drawRect(mStrokeRect, sBackgroundPaint);
sStrokePaint.setColor(mDominantColor);
canvas.drawRoundRect(mStrokeRect, sStrokeWidth, sStrokeWidth, sStrokePaint);
}
/**
* 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
@ -81,9 +138,12 @@ public class FaviconView extends ImageView {
// We assume Favicons are still squares and only bother with the background if more than 3px
// of it would be displayed.
if (Math.abs(mIconBitmap.getWidth() - mActualWidth) > 3) {
showBackground();
mDominantColor = Favicons.getFaviconColor(mIconKey);
if (mDominantColor == -1) {
mDominantColor = 0;
}
} else {
hideBackground();
mDominantColor = 0;
}
}
@ -101,29 +161,6 @@ public class FaviconView extends ImageView {
}
}
/**
* Helper method to display background of the dominant colour of the favicon to pad the remaining
* space.
*/
private void showBackground() {
int color = Favicons.getFaviconColor(mIconKey);
if (color == -1) {
hideBackground();
return;
}
color = Color.argb(70, Color.red(color), Color.green(color), Color.blue(color));
final Drawable drawable = getResources().getDrawable(R.drawable.favicon_bg);
drawable.setColorFilter(color, Mode.SRC_ATOP);
setBackgroundDrawable(drawable);
}
/**
* Method to hide the background. The view will now have a transparent background.
*/
private void hideBackground() {
setBackgroundResource(0);
}
/**
* Sets the icon displayed in this Favicon view to the bitmap provided. If the size of the view
* has been set, the display will be updated right away, otherwise the update will be deferred
@ -158,12 +195,12 @@ public class FaviconView extends ImageView {
public void showDefaultFavicon() {
setImageResource(R.drawable.favicon);
hideBackground();
mDominantColor = 0;
}
private void showNoImage() {
setImageBitmap(null);
hideBackground();
mDominantColor = 0;
}
/**