Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Created August 21, 2012 08:13
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save STAR-ZERO/3413415 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/3413415 to your computer and use it in GitHub Desktop.
【Android】画像を縮小して読み込み、画像リサイズ
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
/**
* 画像変換クラス
*
*/
public class BitmapUtil {
/**
* 画像生成
* 表示サイズ合わせて画像生成時に可能なかぎり縮小して生成します。
*
* @param path パス
* @param width 幅
* @param height 高さ
* @return 生成Bitmap
*/
public static Bitmap createBitmap(String path, int width, int height) {
BitmapFactory.Options option = new BitmapFactory.Options();
// 情報のみ読み込む
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, option);
if (option.outWidth < width || option.outHeight < height) {
// 縦、横のどちらかが指定値より小さい場合は普通にBitmap生成
return BitmapFactory.decodeFile(path);
}
float scaleWidth = ((float) width) / option.outWidth;
float scaleHeight = ((float) height) / option.outHeight;
int newSize = 0;
int oldSize = 0;
if (scaleWidth > scaleHeight) {
newSize = width;
oldSize = option.outWidth;
} else {
newSize = height;
oldSize = option.outHeight;
}
// option.inSampleSizeに設定する値を求める
// option.inSampleSizeは2の乗数のみ設定可能
int sampleSize = 1;
int tmpSize = oldSize;
while (tmpSize > newSize) {
sampleSize = sampleSize * 2;
tmpSize = oldSize / sampleSize;
}
if (sampleSize != 1) {
sampleSize = sampleSize / 2;
}
option.inJustDecodeBounds = false;
option.inSampleSize = sampleSize;
return BitmapFactory.decodeFile(path, option);
}
/**
* 画像リサイズ
* @param bitmap 変換対象ビットマップ
* @param newWidth 変換サイズ横
* @param newHeight 変換サイズ縦
* @return 変換後Bitmap
*/
public static Bitmap resize(Bitmap bitmap, int newWidth, int newHeight) {
if (bitmap == null) {
return null;
}
int oldWidth = bitmap.getWidth();
int oldHeight = bitmap.getHeight();
if (oldWidth < newWidth && oldHeight < newHeight) {
// 縦も横も指定サイズより小さい場合は何もしない
return bitmap;
}
float scaleWidth = ((float) newWidth) / oldWidth;
float scaleHeight = ((float) newHeight) / oldHeight;
float scaleFactor = Math.min(scaleWidth, scaleHeight);
Matrix scale = new Matrix();
scale.postScale(scaleFactor, scaleFactor);
Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, oldWidth, oldHeight, scale, false);
bitmap.recycle();
return resizeBitmap;
}
}
@xu1718191411
Copy link

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment