画像クラスBitmapは、BitmapFactoryクラスより生成します。android OSでは1つのアプリケーションに割り当てられるメモリサイズが非常に小さいため、画像を読み込む際には注意が必要です。
画像ファイル
/res/drawable/image_sample.png
import android.app.Activity;
import android.content.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
public class MyClass extends Activity{
public void myMethod(String path){
// リソースIDから読み込む
Resources rsc = getResources();
Bitmap bmp1 = BitmapFactory.decodeResource(rsc,R.drawable.image_sample);
// 直接ファイル名(path)を指定する場合
Bitmap bmp2 = BitmapFactory.decodeFile(path);
// 画像のサイズ情報のみ取得する
// 幅:opt.outWidth [px] 高さ:opt.outHeight [px]
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true; // 画像サイズ情報のみ取得
BitmapFactory.decodeFile(path,opt);
int width = opt.outWidth();
int height = opt.outHeight();
// オプションを指定して読み込むサイズを調節する
opt.inSampleSize = 4; // 1/4のサイズで読み込む 2,4,8,16 ……
opt.inJustDecodeBounds = false;
Bitmap bmp3 = BitmapFactory.decodeFile(path,opt);
// ビットマップを加工する
// ALPHA_8:Alphaのみ
// RGB_565:Alphaなし
// ARGB_4444:16bit階調
// ARGB_8888:32bit階調
int w = bmp3.getWidth();
int h = bmp3.getHeight();
Bitmap bmp4 = bmp3.copy(Bitmap.Config.ARGB_4444,true);
int[] pixels = new int[w * h];
Bitmap bmp5 = bmp4.getPixels(pixels,0,w,0,0,w,h);
}
}
参考: 音を鳴らす


コメント