画像を拡大縮小する方法

Matrixクラスを使用する方法

Resources r = getResources();
Bitmap bmpSrc,bmpRsz;
Matrix matrix = new Matrix();

// 拡大比率
float rsz_ratio_w = 2.0;
float rsz_ratio_h = 3.0;
// 比率をMatrixに設定
matrix.postScale( rsz_ratio_w, rsz_ratio_h );
// 元画像
bmpSrc = BitmapFactory.decodeResource(r, R.drawable.image_file);
// リサイズ画像
bmpRsz = Bitmap.createBitmap(bmpSrc, 0, 0, bmpSrc.getWidth(),bmpSrc.getHeight(), matrix,true);

Canvasで使用する方法

Canvas canvas = holder.lockCanvas();

Rect src,dst;
Resources res = this.getContext().getResources();
Paint paint = new Paint();

// 元画像
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.image_file);
// 元画像から切り出す位置を指定
src = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
// リサイズ画像の領域を指定
dst = new Rect(0, 0, 150, 100);
// リサイズ画像をCanvasに描画
canvas.drawBitmap(bmp,src,dst,paint);

関連記事

スポンサーリンク

Firefoxのおススメadd-on集

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る