Bug 792410 - Scale tab thumbnails from the upper left corner. r=sriram, a=akeybl
authorWes Johnston <wjohnston@mozilla.com>
Fri, 18 Jan 2013 11:45:10 -0800
changeset 127174 20752324e6e6a23d8a199bef6edd4360a1af09bc
parent 127173 686eab2fdb3a8b0cf5b54c6673793d0aaecce684
child 127175 33542444da17dcc1a89a49a4d5968ed5a9691dc3
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssriram, akeybl
bugs792410
milestone20.0a2
Bug 792410 - Scale tab thumbnails from the upper left corner. r=sriram, a=akeybl
mobile/android/base/GeckoViewsFactory.java
mobile/android/base/Makefile.in
mobile/android/base/resources/layout-xlarge-v11/tabs_row.xml
mobile/android/base/resources/layout/tabs_row.xml
mobile/android/base/widget/ThumbnailView.java
--- a/mobile/android/base/GeckoViewsFactory.java
+++ b/mobile/android/base/GeckoViewsFactory.java
@@ -1,15 +1,16 @@
 /* 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;
 
 import org.mozilla.gecko.gfx.LayerView;
+import org.mozilla.gecko.widget.ThumbnailView;
 
 import android.content.Context;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 
@@ -103,15 +104,17 @@ public final class GeckoViewsFactory imp
             else if (TextUtils.equals(viewName, "LinearLayout"))
                 return new GeckoLinearLayout(context, attrs);
             else if (TextUtils.equals(viewName, "RelativeLayout"))
                 return new GeckoRelativeLayout(context, attrs);
             else if (TextUtils.equals(viewName, "TextSwitcher"))
                 return new GeckoTextSwitcher(context, attrs);
             else if (TextUtils.equals(viewName, "TextView"))
                 return new GeckoTextView(context, attrs);
+            else if (TextUtils.equals(viewName, "widget.ThumbnailView"))
+                return new ThumbnailView(context, attrs);
             else
                 Log.d(LOGTAG, "Warning: unknown custom view: " + viewName);
         }
 
         return null;
     }
 }
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -177,16 +177,17 @@ FENNEC_JAVA_FILES = \
   gfx/ViewTransform.java \
   gfx/VirtualLayer.java \
   ui/Axis.java \
   ui/PanZoomController.java \
   ui/PanZoomTarget.java \
   ui/SimpleScaleGestureDetector.java \
   ui/SubdocumentScrollHelper.java \
   widget/DateTimePicker.java \
+  widget/ThumbnailView.java \
   GeckoNetworkManager.java \
   GeckoScreenOrientationListener.java \
   UpdateService.java \
   GeckoUpdateReceiver.java \
   ReferrerReceiver.java \
   $(NULL)
 
 ifdef MOZ_WEBSMS_BACKEND
--- a/mobile/android/base/resources/layout-xlarge-v11/tabs_row.xml
+++ b/mobile/android/base/resources/layout-xlarge-v11/tabs_row.xml
@@ -17,25 +17,24 @@
     <LinearLayout android:layout_width="fill_parent"
                   android:layout_height="wrap_content">
 
         <RelativeLayout android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_weight="1.0"
                         android:layout_margin="10dip">
 
-            <ImageView android:id="@+id/thumbnail"
+            <org.mozilla.gecko.widget.ThumbnailView android:id="@+id/thumbnail"
                        android:layout_width="@dimen/tab_thumbnail_width"
                        android:layout_height="@dimen/tab_thumbnail_height"
                        android:layout_marginLeft="1dip"
                        android:layout_marginTop="1dip"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
-                       android:src="@drawable/tab_thumbnail_default"
-                       android:scaleType="centerCrop"/>
+                       android:src="@drawable/tab_thumbnail_default"/>
 
             <ImageView android:id="@+id/shadow"
                        android:layout_width="138dip"
                        android:layout_height="80dip"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/tab_thumbnail_shadow"
                        android:scaleType="fitCenter"/>
--- a/mobile/android/base/resources/layout/tabs_row.xml
+++ b/mobile/android/base/resources/layout/tabs_row.xml
@@ -12,25 +12,24 @@
               android:minHeight="@dimen/local_tab_row_height"
               android:background="@drawable/tabs_tray_default_selector"
               android:gravity="center_vertical">
 
     <RelativeLayout android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_margin="10dip">
 
-        <ImageView android:id="@+id/thumbnail"
+        <org.mozilla.gecko.widget.ThumbnailView android:id="@+id/thumbnail"
                    android:layout_width="@dimen/tab_thumbnail_width"
                    android:layout_height="@dimen/tab_thumbnail_height"
                    android:layout_marginLeft="1dip"
                    android:layout_marginTop="1dip"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
-                   android:src="@drawable/tab_thumbnail_default"
-                   android:scaleType="centerCrop"/>
+                   android:src="@drawable/tab_thumbnail_default"/>
 
         <ImageView android:id="@+id/shadow"
                    android:layout_width="138dip"
                    android:layout_height="80dip"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/tab_thumbnail_shadow"
                    android:scaleType="fitCenter"/>
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/widget/ThumbnailView.java
@@ -0,0 +1,63 @@
+/* -*- 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.widget;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.drawable.Drawable;
+import android.graphics.Matrix;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.widget.ImageView;
+
+/* Special version of ImageView for thumbnails. Scales a thumbnail so that it maintains its aspect
+ * ratio and so that the images width and height are the same size or greater than the view size
+ */
+public class ThumbnailView extends ImageView {
+    private static final String LOGTAG = "GeckoThumbnailView";
+    final private Matrix mMatrix;
+    private int mWidthSpec = -1;
+    private int mHeightSpec = -1;
+    private boolean mLayoutChanged;
+
+    public ThumbnailView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        mMatrix = new Matrix();
+        mLayoutChanged = true;
+    }
+
+    @Override
+    public void onDraw(Canvas canvas) {
+        Drawable d = getDrawable();
+        if (mLayoutChanged) {
+            int w1 = d.getIntrinsicWidth();
+            int h1 = d.getIntrinsicHeight();
+            int w2 = getWidth();
+            int h2 = getHeight();
+    
+            float scale = (w2/h2 < w1/h1) ? (float)h2/h1 : (float)w2/w1;
+            mMatrix.setScale(scale, scale);
+        }
+
+        int saveCount = canvas.save();
+        canvas.concat(mMatrix);
+        d.draw(canvas);
+        canvas.restoreToCount(saveCount);
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        // OnLayout.changed isn't a reliable measure of whether or not the size of this view has changed
+        // neither is onSizeChanged called often enough. Instead, we track changes in size ourselves, and
+        // only invalidate this matrix if we have a new width/height spec
+        if (widthMeasureSpec != mWidthSpec || heightMeasureSpec != mHeightSpec) {
+            mWidthSpec = widthMeasureSpec;
+            mHeightSpec = heightMeasureSpec;
+            mLayoutChanged = true;
+        }
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+    }
+}