BitmapDecode 例子主要介紹了Android 平台對映像的解碼功能,Android平台支援PNG, JPEG映像格式,並可以支援 gif動畫。
Android API中用來解碼映像的類主要有BitmapFactory (靜態映像PNG或是JPEG)和Movie 解碼動畫(gif動畫等)。
對映像或動畫解碼,資料來源可以說byte 數組,InputStream ,資源ID,或者指定檔案名稱。對於BitmapFactory來說,還可以通過BitmapFactory.Options 指定解碼時的一些設定。
下面代碼指定opts.inJustDecodeBounds = true,表示解碼時只想擷取被解碼映像的長度和寬度,此時bm傳回值為null, 而opts.outWidth, opts.outHeight中返回了映像的寬度和長度。這種用法解碼器無需為被解碼的映像分配記憶體而值是通過BitmapFactory.Options 的輸出參數返回有關映像的一些資訊。
[java]
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm;
opts.inJustDecodeBounds = true;
bm = BitmapFactory.decodeStream(is, null, opts);
// now opts.outWidth and opts.outHeight are the dimension of the
// bitmap, even though bm is null
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm;
opts.inJustDecodeBounds = true;
bm = BitmapFactory.decodeStream(is, null, opts);
// now opts.outWidth and opts.outHeight are the dimension of the
// bitmap, even though bm is null
下面代碼將採樣大小設為4,相當於每隔4個像素採樣一次,結果是解碼後的映像為原圖的四分之一(具體的採用演算法由平台決定,並非簡單的隔4個像素取其中一個像素值)。
[java]
opts.inJustDecodeBounds = false; // this will request the bm
opts.inSampleSize = 4; // scaled down by 4
bm = BitmapFactory.decodeStream(is, null, opts);
mBitmap = bm;
opts.inJustDecodeBounds = false; // this will request the bm
opts.inSampleSize = 4; // scaled down by 4
bm = BitmapFactory.decodeStream(is, null, opts);
mBitmap = bm;
下面代碼從資源檔通過InputStream解碼映像:
[java]
// decode an image with transparency
is = context.getResources().openRawResource(R.drawable.frog);
mBitmap2 = BitmapFactory.decodeStream(is);
// decode an image with transparency
is = context.getResources().openRawResource(R.drawable.frog);
mBitmap2 = BitmapFactory.decodeStream(is);
對應解碼後的映像Bitmap對象,可以通過getPixels取的映像對應的像素值數組,有了這個像素值數組,Bitmap可以建立不同配置的映像,下面代碼建立兩種配置的映像:ARGB_8888,ARGB_4444,新映像每個像素佔用的位元組大小不一。
[java]
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);
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);
對應Drawable資源來說,更一般的做法是直接使用Resources對象來取得相應的Drawable資源:
[java]
mDrawable = context.getResources().getDrawable
(R.drawable.button);
mDrawable.setBounds(150, 20, 300, 100);
mDrawable = context.getResources().getDrawable
(R.drawable.button);
mDrawable.setBounds(150, 20, 300, 100);
android.graphics.Movie對應可以用來解碼.gif動畫資源,從數組或是直接從InputStream中解碼:R.drawable.animated_gif 為一飄動的旗幟動畫。
[java]
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); www.2cto.com
}
作者:mapdigit