OSDN Git Service

Use FloatMath instead of Math.
authorChih-Chung Chang <chihchung@google.com>
Fri, 10 Feb 2012 23:19:47 +0000 (07:19 +0800)
committerChih-Chung Chang <chihchung@google.com>
Sat, 11 Feb 2012 02:21:58 +0000 (10:21 +0800)
Change-Id: I41661b231f6c034dbca6af26d5950eda6c5fc7da

gallerycommon/src/com/android/gallery3d/common/BitmapUtils.java
src/com/android/gallery3d/app/CropImage.java
src/com/android/gallery3d/app/EyePosition.java
src/com/android/gallery3d/data/LocationClustering.java
src/com/android/gallery3d/photoeditor/RendererUtils.java
src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java
src/com/android/gallery3d/ui/AlbumSlidingWindow.java
src/com/android/gallery3d/ui/CropView.java
src/com/android/gallery3d/ui/PositionController.java
src/com/android/gallery3d/ui/StringTexture.java
src/com/android/gallery3d/ui/TileImageView.java

index 2192cf8..67aa6d7 100644 (file)
@@ -23,6 +23,7 @@ import android.graphics.Canvas;
 import android.graphics.Matrix;
 import android.graphics.Paint;
 import android.os.Build;
+import android.util.FloatMath;
 import android.util.Log;
 
 import java.io.ByteArrayOutputStream;
@@ -71,7 +72,7 @@ public class BitmapUtils {
                 && minSideLength == UNCONSTRAINED) return 1;
 
         int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
-                (int) Math.ceil(Math.sqrt((double) (w * h) / maxNumOfPixels));
+                (int) FloatMath.ceil(FloatMath.sqrt((float) (w * h) / maxNumOfPixels));
 
         if (minSideLength == UNCONSTRAINED) {
             return lowerBound;
@@ -95,7 +96,7 @@ public class BitmapUtils {
 
     // Fin the min x that 1 / x <= scale
     public static int computeSampleSizeLarger(float scale) {
-        int initialSize = (int) Math.floor(1f / scale);
+        int initialSize = (int) FloatMath.floor(1f / scale);
         if (initialSize <= 1) return 1;
 
         return initialSize <= 8
@@ -106,7 +107,7 @@ public class BitmapUtils {
     // Find the max x that 1 / x >= scale.
     public static int computeSampleSize(float scale) {
         Utils.assertTrue(scale > 0);
-        int initialSize = Math.max(1, (int) Math.ceil(1 / scale));
+        int initialSize = Math.max(1, (int) FloatMath.ceil(1 / scale));
         return initialSize <= 8
                 ? Utils.nextPowerOf2(initialSize)
                 : (initialSize + 7) / 8 * 8;
@@ -116,8 +117,7 @@ public class BitmapUtils {
             Bitmap bitmap, int targetPixels, boolean recycle) {
         int width = bitmap.getWidth();
         int height = bitmap.getHeight();
-        float scale = (float) Math.sqrt(
-                (double) targetPixels / (width * height));
+        float scale = FloatMath.sqrt((float) targetPixels / (width * height));
         if (scale >= 1.0f) return bitmap;
         return resizeBitmapByScale(bitmap, scale, recycle);
     }
index 5ac9f02..7e1572f 100644 (file)
@@ -38,6 +38,7 @@ import android.os.Handler;
 import android.os.Message;
 import android.provider.MediaStore;
 import android.provider.MediaStore.Images;
+import android.util.FloatMath;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.Window;
@@ -554,8 +555,7 @@ public class CropImage extends AbstractGalleryActivity {
         }
 
         if (outputX * outputY > MAX_PIXEL_COUNT) {
-            float scale = (float) Math.sqrt(
-                    (double) MAX_PIXEL_COUNT / outputX / outputY);
+            float scale = FloatMath.sqrt((float) MAX_PIXEL_COUNT / outputX / outputY);
             Log.w(TAG, "scale down the cropped image: " + scale);
             outputX = Math.round(scale * outputX);
             outputY = Math.round(scale * outputY);
index 7b4495a..f9713e1 100644 (file)
@@ -25,6 +25,7 @@ import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.os.SystemClock;
+import android.util.FloatMath;
 import android.view.Display;
 import android.view.Surface;
 import android.view.WindowManager;
@@ -41,9 +42,9 @@ public class EyePosition {
     private static final int GYROSCOPE_SETTLE_DOWN = 15;
     private static final float GYROSCOPE_RESTORE_FACTOR = 0.995f;
 
-    private static final double USER_ANGEL = Math.toRadians(10);
-    private static final float USER_ANGEL_COS = (float) Math.cos(USER_ANGEL);
-    private static final float USER_ANGEL_SIN = (float) Math.sin(USER_ANGEL);
+    private static final float USER_ANGEL = (float) Math.toRadians(10);
+    private static final float USER_ANGEL_COS = FloatMath.cos(USER_ANGEL);
+    private static final float USER_ANGEL_SIN = FloatMath.sin(USER_ANGEL);
     private static final float MAX_VIEW_RANGE = (float) 0.5;
     private static final int NOT_STARTED = -1;
 
@@ -126,8 +127,8 @@ public class EyePosition {
         float ty = -1 + t * y;
         float tz = t * z;
 
-        float length = (float) Math.sqrt(tx * tx + ty * ty + tz * tz);
-        float glength = (float) Math.sqrt(temp);
+        float length = FloatMath.sqrt(tx * tx + ty * ty + tz * tz);
+        float glength = FloatMath.sqrt(temp);
 
         mX = Utils.clamp((x * USER_ANGEL_COS / glength
                 + tx * USER_ANGEL_SIN / length) * mUserDistance,
@@ -135,7 +136,7 @@ public class EyePosition {
         mY = -Utils.clamp((y * USER_ANGEL_COS / glength
                 + ty * USER_ANGEL_SIN / length) * mUserDistance,
                 -mLimit, mLimit);
-        mZ = (float) -Math.sqrt(
+        mZ = -FloatMath.sqrt(
                 mUserDistance * mUserDistance - mX * mX - mY * mY);
         mListener.onEyePositionChanged(mX, mY, mZ);
     }
@@ -173,7 +174,7 @@ public class EyePosition {
         mY = Utils.clamp((float) (mY + y * t / Math.hypot(mZ, mY)),
                 -mLimit, mLimit) * GYROSCOPE_RESTORE_FACTOR;
 
-        mZ = (float) -Math.sqrt(
+        mZ = -FloatMath.sqrt(
                 mUserDistance * mUserDistance - mX * mX - mY * mY);
         mListener.onEyePositionChanged(mX, mY, mZ);
     }
index 788060c..9f3a358 100644 (file)
@@ -23,6 +23,7 @@ import com.android.gallery3d.util.GalleryUtils;
 import android.content.Context;
 import android.os.Handler;
 import android.os.Looper;
+import android.util.FloatMath;
 import android.widget.Toast;
 
 import java.util.ArrayList;
@@ -294,7 +295,7 @@ class LocationClustering extends Clustering {
             }
 
             // step 5: calculate the final score
-            float score = totalDistance * (float) Math.sqrt(realK);
+            float score = totalDistance * FloatMath.sqrt(realK);
 
             if (score < bestScore) {
                 bestScore = score;
index b92907d..3edcff5 100644 (file)
@@ -19,6 +19,7 @@ package com.android.gallery3d.photoeditor;
 import android.graphics.Bitmap;
 import android.opengl.GLES20;
 import android.opengl.GLUtils;
+import android.util.FloatMath;
 
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
@@ -153,8 +154,8 @@ public class RendererUtils {
     public static void setRenderToRotate(RenderContext context, int srcWidth, int srcHeight,
             int dstWidth, int dstHeight, float degrees) {
         float radian = -degrees * DEGREE_TO_RADIAN;
-        float cosTheta = (float) Math.cos(radian);
-        float sinTheta = (float) Math.sin(radian);
+        float cosTheta = FloatMath.cos(radian);
+        float sinTheta = FloatMath.sin(radian);
         float cosWidth = cosTheta * srcWidth;
         float sinWidth = sinTheta * srcWidth;
         float cosHeight = cosTheta * srcHeight;
@@ -205,8 +206,8 @@ public class RendererUtils {
         System.arraycopy(base, 0, vertices, 0, vertices.length);
         if (horizontalDegrees % 180f != 0) {
             float radian = (horizontalDegrees - horizontalRounds * 180) * DEGREE_TO_RADIAN;
-            float cosTheta = (float) Math.cos(radian);
-            float sinTheta = (float) Math.sin(radian);
+            float cosTheta = FloatMath.cos(radian);
+            float sinTheta = FloatMath.sin(radian);
 
             float scale = length / (length + sinTheta * base[0]);
             vertices[0] = cosTheta * base[0] * scale;
@@ -223,8 +224,8 @@ public class RendererUtils {
 
         if (verticalDegrees % 180f != 0) {
             float radian = (verticalDegrees - verticalRounds * 180) * DEGREE_TO_RADIAN;
-            float cosTheta = (float) Math.cos(radian);
-            float sinTheta = (float) Math.sin(radian);
+            float cosTheta = FloatMath.cos(radian);
+            float sinTheta = FloatMath.sin(radian);
 
             float scale = length / (length + sinTheta * base[1]);
             vertices[0] = base[0] * scale;
index 87ff557..0f03da8 100644 (file)
@@ -19,6 +19,7 @@ package com.android.gallery3d.ui;
 import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.os.Message;
+import android.util.FloatMath;
 
 import com.android.gallery3d.R;
 import com.android.gallery3d.app.GalleryActivity;
@@ -381,8 +382,8 @@ public class AlbumSetSlidingWindow implements AlbumSetView.ModelListener {
             float scaley = mBoxHeight / (float) height;
             float scale = Math.min(scalex, scaley);
 
-            width = (int) Math.floor(width * scale);
-            height = (int) Math.floor(height * scale);
+            width = (int) FloatMath.floor(width * scale);
+            height = (int) FloatMath.floor(height * scale);
 
             // Now draw it
             int sourceType = SelectionDrawer.DATASOURCE_TYPE_NOT_CATEGORIZED;
index b40d72c..6947d7f 100644 (file)
@@ -18,6 +18,7 @@ package com.android.gallery3d.ui;
 
 import android.graphics.Bitmap;
 import android.os.Message;
+import android.util.FloatMath;
 
 import com.android.gallery3d.app.GalleryActivity;
 import com.android.gallery3d.common.BitmapUtils;
@@ -329,8 +330,8 @@ public class AlbumSlidingWindow implements AlbumView.ModelListener {
             float scaley = mBoxHeight / (float) height;
             float scale = Math.min(scalex, scaley);
 
-            width = (int) Math.floor(width * scale);
-            height = (int) Math.floor(height * scale);
+            width = (int) FloatMath.floor(width * scale);
+            height = (int) FloatMath.floor(height * scale);
 
             // Now draw it
             if (pass == 0) {
index 71fdaab..227e67e 100644 (file)
@@ -31,6 +31,7 @@ import android.graphics.RectF;
 import android.media.FaceDetector;
 import android.os.Handler;
 import android.os.Message;
+import android.util.FloatMath;
 import android.view.MotionEvent;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.Toast;
@@ -756,8 +757,7 @@ public class CropView extends GLView {
         int rotation = mImageRotation;
         int width = bitmap.getWidth();
         int height = bitmap.getHeight();
-        float scale = (float) Math.sqrt(
-                (double) FACE_PIXEL_COUNT / (width * height));
+        float scale = FloatMath.sqrt((float) FACE_PIXEL_COUNT / (width * height));
 
         // faceBitmap is a correctly rotated bitmap, as viewed by a user.
         Bitmap faceBitmap;
index b4dac97..734c571 100644 (file)
@@ -29,6 +29,7 @@ import android.graphics.Color;
 import android.graphics.RectF;
 import android.os.Message;
 import android.os.SystemClock;
+import android.util.FloatMath;
 import android.view.GestureDetector;
 import android.view.MotionEvent;
 import android.view.ScaleGestureDetector;
@@ -406,7 +407,7 @@ class PositionController {
         // force it to be in the center.
         // (We do for height only, not width, because the user may
         // want to scroll to the previous/next image.)
-        if (Math.floor(mImageH * mToScale) <= mViewH) {
+        if (FloatMath.floor(mImageH * mToScale) <= mViewH) {
             mToY = mImageH / 2;
         }
 
@@ -582,19 +583,19 @@ class PositionController {
     private void calculateStableBound(float scale, float horizontalSlack) {
         // The number of pixels between the center of the view
         // and the edge when the edge is aligned.
-        mBoundLeft = (int) Math.ceil((mViewW - horizontalSlack) / (2 * scale));
+        mBoundLeft = (int) FloatMath.ceil((mViewW - horizontalSlack) / (2 * scale));
         mBoundRight = mImageW - mBoundLeft;
-        mBoundTop = (int) Math.ceil(mViewH / (2 * scale));
+        mBoundTop = (int) FloatMath.ceil(mViewH / (2 * scale));
         mBoundBottom = mImageH - mBoundTop;
 
         // If the scaled height is smaller than the view height,
         // force it to be in the center.
-        if (Math.floor(mImageH * scale) <= mViewH) {
+        if (FloatMath.floor(mImageH * scale) <= mViewH) {
             mBoundTop = mBoundBottom = mImageH / 2;
         }
 
         // Same for width
-        if (Math.floor(mImageW * scale) <= mViewW) {
+        if (FloatMath.floor(mImageW * scale) <= mViewW) {
             mBoundLeft = mBoundRight = mImageW / 2;
         }
     }
index f576c01..935cd72 100644 (file)
@@ -21,6 +21,7 @@ import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint.FontMetricsInt;
 import android.graphics.Typeface;
+import android.util.FloatMath;
 import android.text.TextPaint;
 import android.text.TextUtils;
 
@@ -69,7 +70,7 @@ class StringTexture extends CanvasTexture {
 
     private static StringTexture newInstance(String text, TextPaint paint) {
         FontMetricsInt metrics = paint.getFontMetricsInt();
-        int width = (int) Math.ceil(paint.measureText(text));
+        int width = (int) FloatMath.ceil(paint.measureText(text));
         int height = metrics.bottom - metrics.top;
         // The texture size needs to be at least 1x1.
         if (width <= 0) width = 1;
index 980f7b2..05d09f5 100644 (file)
@@ -19,6 +19,7 @@ package com.android.gallery3d.ui;
 import android.graphics.Bitmap;
 import android.graphics.Rect;
 import android.graphics.RectF;
+import android.util.FloatMath;
 
 import com.android.gallery3d.app.GalleryContext;
 import com.android.gallery3d.common.Utils;
@@ -293,10 +294,10 @@ public class TileImageView extends GLView {
         int height = (int) Math.ceil(Math.max(
                 Math.abs(sin * w + cos * h), Math.abs(sin * w - cos * h)));
 
-        int left = (int) Math.floor(cX - width / (2f * scale));
-        int top = (int) Math.floor(cY - height / (2f * scale));
-        int right = (int) Math.ceil(left + width / scale);
-        int bottom = (int) Math.ceil(top + height / scale);
+        int left = (int) FloatMath.floor(cX - width / (2f * scale));
+        int top = (int) FloatMath.floor(cY - height / (2f * scale));
+        int right = (int) FloatMath.ceil(left + width / scale);
+        int bottom = (int) FloatMath.ceil(top + height / scale);
 
         // align the rectangle to tile boundary
         int size = TILE_SIZE << level;