Merge "fix [2515291] Native crash and runtime restart while trying to preview captured picture on Sapphire"
diff --git a/core/java/android/widget/ImageView.java b/core/java/android/widget/ImageView.java
index c81a907..c77416b 100644
--- a/core/java/android/widget/ImageView.java
+++ b/core/java/android/widget/ImageView.java
@@ -769,8 +769,8 @@
             } else if (ScaleType.CENTER == mScaleType) {
                 // Center bitmap in view, no scaling.
                 mDrawMatrix = mMatrix;
-                mDrawMatrix.setTranslate((vwidth - dwidth) * 0.5f,
-                                         (vheight - dheight) * 0.5f);
+                mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * 0.5f + 0.5f),
+                                         (int) ((vheight - dheight) * 0.5f + 0.5f));
             } else if (ScaleType.CENTER_CROP == mScaleType) {
                 mDrawMatrix = mMatrix;
 
@@ -786,7 +786,7 @@
                 }
 
                 mDrawMatrix.setScale(scale, scale);
-                mDrawMatrix.postTranslate(dx, dy);
+                mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
             } else if (ScaleType.CENTER_INSIDE == mScaleType) {
                 mDrawMatrix = mMatrix;
                 float scale;
@@ -800,8 +800,8 @@
                             (float) vheight / (float) dheight);
                 }
                 
-                dx = (vwidth - dwidth * scale) * 0.5f;
-                dy = (vheight - dheight * scale) * 0.5f;
+                dx = (int) ((vwidth - dwidth * scale) * 0.5f + 0.5f);
+                dy = (int) ((vheight - dheight * scale) * 0.5f + 0.5f);
 
                 mDrawMatrix.setScale(scale, scale);
                 mDrawMatrix.postTranslate(dx, dy);
diff --git a/core/java/com/android/internal/view/menu/IconMenuView.java b/core/java/com/android/internal/view/menu/IconMenuView.java
index bba2ee2..beb57ba 100644
--- a/core/java/com/android/internal/view/menu/IconMenuView.java
+++ b/core/java/com/android/internal/view/menu/IconMenuView.java
@@ -179,6 +179,10 @@
      */
     private void layoutItems(int width) {
         int numItems = getChildCount();
+        if (numItems == 0) {
+            mLayoutNumRows = 0;
+            return;
+        }
         
         // Start with the least possible number of rows
         int curNumRows =
@@ -470,15 +474,18 @@
         
         // Get the desired height of the icon menu view (last row of items does
         // not have a divider below)
+        final int layoutNumRows = mLayoutNumRows;
         final int desiredHeight = (mRowHeight + mHorizontalDividerHeight) *
-                mLayoutNumRows - mHorizontalDividerHeight;
+                layoutNumRows - mHorizontalDividerHeight;
         
         // Maximum possible width and desired height
         setMeasuredDimension(measuredWidth,
                 resolveSize(desiredHeight, heightMeasureSpec));
 
         // Position the children
-        positionChildren(mMeasuredWidth, mMeasuredHeight);
+        if (layoutNumRows > 0) {
+            positionChildren(mMeasuredWidth, mMeasuredHeight);
+        }
     }