1 package org.openintents.filemanager.util;
\r
3 import android.graphics.Bitmap;
\r
4 import android.graphics.Canvas;
\r
5 import android.graphics.Matrix;
\r
6 import android.graphics.drawable.BitmapDrawable;
\r
7 import android.graphics.drawable.Drawable;
\r
9 public final class ImageUtils {
\r
12 * Resizes specific a Bitmap with keeping ratio.
\r
14 public static Bitmap resizeBitmap(Bitmap drawable, int desireWidth,
\r
16 int width = drawable.getWidth();
\r
17 int height = drawable.getHeight();
\r
19 if (0 < width && 0 < height && desireWidth < width
\r
20 || desireHeight < height) {
\r
23 if (width < height) {
\r
24 scale = (float) desireHeight / (float) height;
\r
25 if (desireWidth < width * scale) {
\r
26 scale = (float) desireWidth / (float) width;
\r
29 scale = (float) desireWidth / (float) width;
\r
32 // Draw resized image
\r
33 Matrix matrix = new Matrix();
\r
34 matrix.postScale(scale, scale);
\r
35 Bitmap bitmap = Bitmap.createBitmap(drawable, 0, 0, width, height,
\r
37 Canvas canvas = new Canvas(bitmap);
\r
38 canvas.drawBitmap(bitmap, 0, 0, null);
\r
47 * Resizes specific a Drawable with keeping ratio.
\r
49 public static Drawable resizeDrawable(Drawable drawable, int desireWidth,
\r
51 int width = drawable.getIntrinsicWidth();
\r
52 int height = drawable.getIntrinsicHeight();
\r
54 if (0 < width && 0 < height && desireWidth < width
\r
55 || desireHeight < height) {
\r
56 drawable = new BitmapDrawable(resizeBitmap(
\r
57 ((BitmapDrawable) drawable).getBitmap(), desireWidth,
\r