티스토리 뷰

3. Decoder
. 이미지 읽어오기
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm;

opts.inJustDecodeBounds = true; //이런식으로 이미지를 읽어 온다. 메모리 픽셀을 허용안함.
bm = BitmapFactory.decodeStream(is, null, opts);

opts.inJustDecodeBounds = false; // this will request the bm 메모리 픽셀을 허용
opts.inSampleSize = 4; // 1:동일한 사이즈 4: width/height의 1/4 사이즈
bm = BitmapFactory.decodeStream(is, null, opts);


. decode an image with transparency
is = context.getResources().openRawResource(R.drawable.frog);
mBitmap2 = BitmapFactory.decodeStream(is);

.create a deep copy of it using getPixels() into different configs
픽셀값으로 새로운(비트수가 다른) 비트맵을 만들수잇다.
int w = mBitmap2.getWidth();
int h = mBitmap2.getHeight();
int[] pixels = new int[w*h];
mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h);
mBitmap3 = Bitmap.createBitmap(pixels, 0, w, w, h,
Bitmap.Config.ARGB_8888);
mBitmap4 = Bitmap.createBitmap(pixels, 0, w, w, h,
Bitmap.Config.ARGB_4444);

. 움직이는 Animation GIF 읽어 오기
private Movie mMovie;
is = context.getResources().openRawResource(R.drawable.animated_gif);
if (true) {
mMovie = Movie.decodeStream(is); //이런 방식도 있음
} else {
byte[] array = streamToBytes(is); //요로한 방식도 있음
mMovie = Movie.decodeByteArray(array, 0, array.length);
}

. 움직임 그리기
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int)((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() - mMovie.width(),
getHeight() - mMovie.height());
invalidate();
}

. 단점 stream의 사이즈를 알수가 없다. 사이즈에 대한 정보를 따로 주어야 한다.
private static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}

. drawable로 이미지 그리기
mDrawable = context.getResources().getDrawable(R.drawable.button);
mDrawable.setBounds(150, 20, 300, 100); //위치 지정

캔버스에 drawable를 그린다.
mDrawable.draw(canvas);

'프로그래밍 언어 > android' 카테고리의 다른 글

스피드 랭킹퍼즐  (0) 2024.03.21
sound  (0) 2010.01.28
animation  (0) 2010.01.20
비트맵 그리기  (0) 2009.10.29
FrameLayout 에서 Thread를 이용한 이미지(또는 애니메이트) 그리기  (0) 2009.10.29
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함