bug 721032 - Make thumbnails in about:home and tab menu identical r=mfinkle

This commit is contained in:
Brad Lassey 2012-01-26 01:02:00 -05:00
parent 9fd5aa9d7c
commit 81cf25f4e0
3 changed files with 73 additions and 22 deletions

View File

@ -55,11 +55,13 @@ import java.util.List;
public final class Tab { public final class Tab {
private static final String LOGTAG = "GeckoTab"; private static final String LOGTAG = "GeckoTab";
private static final int kThumbnailWidth = 120; private static final int kThumbnailWidth = 136;
private static final int kThumbnailHeight = 80; private static final int kThumbnailHeight = 77;
private static int sMinDim = 0; private static float sMinDim = 0;
private static float sDensity = 1; private static float sDensity = 1;
private static int sMinScreenshotWidth = 0;
private static int sMinScreenshotHeight = 0;
private int mId; private int mId;
private String mUrl; private String mUrl;
private String mTitle; private String mTitle;
@ -145,24 +147,73 @@ public final class Tab {
return mThumbnail; return mThumbnail;
} }
void initMetrics() {
DisplayMetrics metrics = new DisplayMetrics();
GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
sMinDim = Math.min(metrics.widthPixels / kThumbnailWidth, metrics.heightPixels / kThumbnailHeight);
sDensity = metrics.density;
}
float getMinDim() {
if (sMinDim == 0)
initMetrics();
return sMinDim;
}
float getDensity() {
if (sDensity == 0.0f)
initMetrics();
return sDensity;
}
int getMinScreenshotWidth() {
if (sMinScreenshotWidth != 0)
return sMinScreenshotWidth;
return sMinScreenshotWidth = (int)(getMinDim() * kThumbnailWidth);
}
int getMinScreenshotHeight() {
if (sMinScreenshotHeight != 0)
return sMinScreenshotHeight;
return sMinScreenshotHeight = (int)(getMinDim() * kThumbnailHeight);
}
int getThumbnailWidth() {
return (int)(kThumbnailWidth * getDensity());
}
int getThumbnailHeight() {
return (int)(kThumbnailHeight * getDensity());
}
public void updateThumbnail(final Bitmap b) { public void updateThumbnail(final Bitmap b) {
final Tab tab = this; final Tab tab = this;
GeckoAppShell.getHandler().post(new Runnable() { GeckoAppShell.getHandler().post(new Runnable() {
public void run() { public void run() {
if (sMinDim == 0) {
DisplayMetrics metrics = new DisplayMetrics();
GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
sMinDim = Math.min(metrics.widthPixels / 3, metrics.heightPixels / 2);
sDensity = metrics.density;
}
if (b != null) { if (b != null) {
try { try {
Bitmap cropped = Bitmap.createBitmap(b, 0, 0, sMinDim * 3, sMinDim * 2); Bitmap cropped = null;
Bitmap bitmap = Bitmap.createScaledBitmap(cropped, (int) (kThumbnailWidth * sDensity), (int) (kThumbnailHeight * sDensity), false); /* Crop to screen width if the bitmap is larger than the screen width or height. If smaller and the
saveThumbnailToDB(new BitmapDrawable(bitmap)); * the aspect ratio is correct, just use the bitmap as is. Otherwise, fit the smaller
b.recycle(); * smaller dimension, then crop the larger dimention.
*/
if (getMinScreenshotWidth() < b.getWidth() && getMinScreenshotHeight() < b.getHeight())
cropped = Bitmap.createBitmap(b, 0, 0, getMinScreenshotWidth(), getMinScreenshotHeight());
else if (b.getWidth() * getMinScreenshotHeight() == b.getHeight() * getMinScreenshotWidth())
cropped = b;
else if (b.getWidth() * getMinScreenshotHeight() < b.getHeight() * getMinScreenshotWidth())
cropped = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getWidth() * getMinScreenshotHeight() / getMinScreenshotWidth());
else
cropped = Bitmap.createBitmap(b, 0, 0,
b.getHeight() * getMinScreenshotWidth() / getMinScreenshotHeight(),
b.getHeight());
bitmap = Bitmap.createBitmap(cropped, 0, 0, (int) (138 * sDensity), (int) (78 * sDensity)); Bitmap bitmap = Bitmap.createScaledBitmap(cropped, getThumbnailWidth(), getThumbnailHeight(), false);
saveThumbnailToDB(new BitmapDrawable(bitmap));
if (!cropped.equals(b))
b.recycle();
mThumbnail = new BitmapDrawable(bitmap); mThumbnail = new BitmapDrawable(bitmap);
cropped.recycle(); cropped.recycle();
} catch (OutOfMemoryError oom) { } catch (OutOfMemoryError oom) {

View File

@ -8,15 +8,15 @@
android:layout_height="fill_parent"> android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" <LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_width="138dip"
android:layout_height="wrap_content" android:layout_height="80dip"
android:background="@drawable/abouthome_topsite_shadow" android:background="@drawable/abouthome_topsite_shadow"
android:padding="1dip" android:padding="1dip"
android:paddingTop="2dip"> android:paddingBottom="2dip">
<ImageView android:id="@+id/thumbnail" <ImageView android:id="@+id/thumbnail"
android:layout_width="fill_parent" android:layout_width="136dip"
android:layout_height="80dip" android:layout_height="77dip"
android:scaleType="centerCrop"/> android:scaleType="centerCrop"/>
</LinearLayout> </LinearLayout>

View File

@ -7,8 +7,8 @@
android:background="@drawable/tabs_tray_list_selector"> android:background="@drawable/tabs_tray_list_selector">
<ImageView android:id="@+id/thumbnail" <ImageView android:id="@+id/thumbnail"
android:layout_width="138dip" android:layout_width="136dip"
android:layout_height="78dip" android:layout_height="77dip"
android:layout_marginLeft="35dip" android:layout_marginLeft="35dip"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
@ -16,7 +16,7 @@
android:scaleType="fitCenter"/> android:scaleType="fitCenter"/>
<ImageView android:id="@+id/shadow" <ImageView android:id="@+id/shadow"
android:layout_width="140dip" android:layout_width="138dip"
android:layout_height="80dip" android:layout_height="80dip"
android:layout_marginLeft="34dip" android:layout_marginLeft="34dip"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"