--- /dev/null
+package org.openintents.filemanager.util;\r
+\r
+import android.graphics.Bitmap;\r
+import android.graphics.Canvas;\r
+import android.graphics.Matrix;\r
+import android.graphics.drawable.BitmapDrawable;\r
+import android.graphics.drawable.Drawable;\r
+\r
+public final class ImageUtils {\r
+\r
+ /**\r
+ * Resizes specific a Bitmap with keeping ratio.\r
+ */\r
+ public static Bitmap resizeBitmap(Bitmap drawable, int desireWidth,\r
+ int desireHeight) {\r
+ int width = drawable.getWidth();\r
+ int height = drawable.getHeight();\r
+\r
+ if (0 < width && 0 < height && desireWidth < width\r
+ || desireHeight < height) {\r
+ // Calculate scale\r
+ float scale;\r
+ if (width < height) {\r
+ scale = (float) desireHeight / (float) height;\r
+ if (desireWidth < width * scale) {\r
+ scale = (float) desireWidth / (float) width;\r
+ }\r
+ } else {\r
+ scale = (float) desireWidth / (float) width;\r
+ }\r
+\r
+ // Draw resized image\r
+ Matrix matrix = new Matrix();\r
+ matrix.postScale(scale, scale);\r
+ Bitmap bitmap = Bitmap.createBitmap(drawable, 0, 0, width, height,\r
+ matrix, true);\r
+ Canvas canvas = new Canvas(bitmap);\r
+ canvas.drawBitmap(bitmap, 0, 0, null);\r
+\r
+ drawable = bitmap;\r
+ }\r
+\r
+ return drawable;\r
+ }\r
+\r
+ /**\r
+ * Resizes specific a Drawable with keeping ratio.\r
+ */\r
+ public static Drawable resizeDrawable(Drawable drawable, int desireWidth,\r
+ int desireHeight) {\r
+ int width = drawable.getIntrinsicWidth();\r
+ int height = drawable.getIntrinsicHeight();\r
+\r
+ if (0 < width && 0 < height && desireWidth < width\r
+ || desireHeight < height) {\r
+ drawable = new BitmapDrawable(resizeBitmap(\r
+ ((BitmapDrawable) drawable).getBitmap(), desireWidth,\r
+ desireHeight));\r
+ }\r
+\r
+ return drawable;\r
+ }\r
+\r
+}\r