Android ApiDemos樣本解析(54):Graphics->BitmapDecode

來源:互聯網
上載者:User

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
 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.