mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 888326 - Part 5: Make the FaviconView able to resize strangely sized inputs to more appropriate sizes, if enabled. r=mleibovic
This commit is contained in:
parent
5cb3cb8044
commit
b9fadbce0d
@ -13,43 +13,152 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff.Mode;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ImageView;
|
||||
|
||||
/*
|
||||
/**
|
||||
* Special version of ImageView for favicons.
|
||||
* Changes image background depending on favicon size.
|
||||
* 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 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
|
||||
// string is acceptable.
|
||||
private String mIconKey;
|
||||
|
||||
private int mActualWidth;
|
||||
private int mActualHeight;
|
||||
|
||||
// Flag indicating if the most recently assigned image is considered likely to need scaling.
|
||||
private boolean mScalingExpected;
|
||||
|
||||
public FaviconView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setScaleType(ImageView.ScaleType.CENTER);
|
||||
}
|
||||
|
||||
/*
|
||||
* @param bitmap favicon image
|
||||
* @param key string used as a key to cache the dominant color of this image
|
||||
@Override
|
||||
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
|
||||
super.onSizeChanged(xNew, yNew, xOld, yOld);
|
||||
|
||||
// No point rechecking the image if there hasn't really been any change.
|
||||
if (xNew == mActualHeight && yNew == mActualWidth) {
|
||||
return;
|
||||
}
|
||||
mActualWidth = xNew;
|
||||
mActualHeight = yNew;
|
||||
formatImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public void updateImage(Bitmap bitmap, String key) {
|
||||
if (bitmap == null) {
|
||||
// If the bitmap is null, show the default favicon.
|
||||
setImageResource(R.drawable.favicon);
|
||||
// The default favicon image is hi-res, so we should hide the background.
|
||||
setBackgroundResource(0);
|
||||
} else if (Favicons.isLargeFavicon(bitmap)) {
|
||||
setImageBitmap(bitmap);
|
||||
// If the icon is large, hide the background.
|
||||
setBackgroundResource(0);
|
||||
private void formatImage() {
|
||||
// If we're called before bitmap is set, just show the default.
|
||||
if (mIconBitmap == null) {
|
||||
setImageResource(0);
|
||||
hideBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
// If we're called before size set, abort.
|
||||
if (mActualWidth == 0 || mActualHeight == 0) {
|
||||
hideBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mScalingExpected && mActualWidth != mIconBitmap.getWidth()) {
|
||||
scaleBitmap();
|
||||
// Don't scale the image every time something changes.
|
||||
mScalingExpected = false;
|
||||
}
|
||||
|
||||
setImageBitmap(mIconBitmap);
|
||||
|
||||
// After scaling, determine if we have empty space around the scaled image which we need to
|
||||
// fill with the coloured background. If applicable, show it.
|
||||
// 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();
|
||||
} else {
|
||||
setImageBitmap(bitmap);
|
||||
// Otherwise show a dominant color background.
|
||||
int color = Favicons.getFaviconColor(bitmap, key);
|
||||
color = Color.argb(70, Color.red(color), Color.green(color), Color.blue(color));
|
||||
Drawable drawable = getResources().getDrawable(R.drawable.favicon_bg);
|
||||
drawable.setColorFilter(color, Mode.SRC_ATOP);
|
||||
setBackgroundDrawable(drawable);
|
||||
hideBackground();
|
||||
}
|
||||
}
|
||||
|
||||
private void scaleBitmap() {
|
||||
// If the Favicon can be resized to fill the view exactly without an enlargment of more than
|
||||
// a factor of two, do so.
|
||||
int doubledSize = mIconBitmap.getWidth()*2;
|
||||
if (mActualWidth > doubledSize) {
|
||||
// If the view is more than twice the size of the image, just double the image size
|
||||
// and do the rest with padding.
|
||||
mIconBitmap = Bitmap.createScaledBitmap(mIconBitmap, doubledSize, doubledSize, true);
|
||||
} else {
|
||||
// Otherwise, scale the image to fill the view.
|
||||
mIconBitmap = Bitmap.createScaledBitmap(mIconBitmap, mActualWidth, mActualWidth, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to display background of the dominant colour of the favicon to pad the remaining
|
||||
* space.
|
||||
*/
|
||||
private void showBackground() {
|
||||
int color = Favicons.getFaviconColor(mIconBitmap, mIconKey);
|
||||
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
|
||||
* until then. The key provided is used to cache the result of the calculation of the dominant
|
||||
* colour of the provided image - this value is used to draw the coloured background in this view
|
||||
* if the icon is not large enough to fill it.
|
||||
*
|
||||
* @param bitmap favicon image
|
||||
* @param key string used as a key to cache the dominant color of this image
|
||||
* @param allowScaling If true, allows the provided bitmap to be scaled by this FaviconView.
|
||||
* Typically, you should prefer using Favicons obtained via the caching system
|
||||
* (Favicons class), so as to exploit caching.
|
||||
*/
|
||||
private void updateImageInternal(Bitmap bitmap, String key, boolean allowScaling) {
|
||||
// Reassigning the same bitmap? Don't bother.
|
||||
if (mUnscaledBitmap == bitmap) {
|
||||
return;
|
||||
}
|
||||
mUnscaledBitmap = bitmap;
|
||||
mIconBitmap = bitmap;
|
||||
mIconKey = key;
|
||||
mScalingExpected = allowScaling;
|
||||
|
||||
// Possibly update the display.
|
||||
formatImage();
|
||||
}
|
||||
|
||||
public void updateAndScaleImage(Bitmap bitmap, String key) {
|
||||
updateImageInternal(bitmap, key, true);
|
||||
}
|
||||
|
||||
public void updateImage(Bitmap bitmap, String key) {
|
||||
updateImageInternal(bitmap, key, false);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user