天天看點

assets原始資源的繼續使用及Bitmap和BitmapFactory的使用

随時随地閱讀更多技術實戰幹貨,擷取項目源碼、學習資料,請關注源代碼社群公衆号(ydmsq666)

assets原始資源的繼續使用及Bitmap和BitmapFactory的使用

本文中既讀取了assets中的原始資源,又練習了Bitmap和BitmapFactory的使用。

Bitmap代表一張位圖,BitmapDrawable裡封裝的圖檔就是一個Bitmap對象,把一個Bitmap對象包裝成BitmapDrawable對象,可以使用BitmapDrawable的構造器:

BitmapDrawable drawable=new BitmapDrawable(bitmap);

擷取一個BitmapDrawable所包裝的Bitmap對象:

Bitmap bitmap=drawable.getBitmap();

另外,Bitmap還提供了一些靜态方法來建立新的Bitmap對象,如下:

assets原始資源的繼續使用及Bitmap和BitmapFactory的使用

注意:由于手機系統的記憶體比較小,如果系統不停地去解析、建立Bitmap對象,可能由于前面建立的Bitmap所占用的記憶體還沒有回收而導緻程式運作引發OutOfMemory錯誤。是以Android使用下面兩個方法分别判斷Bitmap是否已經回收和強制回收:

boolean isRecycled():傳回該Bitmap對象是否已被回收。

void recycle():強制一個Bitmap對象立即回收自己。

下面開發一個檢視assets目錄下圖檔的圖檔檢視器,實作自動搜尋該目錄下的下一張圖檔,代碼如下:

Activity:

package com.lovo.bitmaptest;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	String[] images = null;
	AssetManager assets = null;
	int currentImg = 0;
	ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		image = (ImageView) findViewById(R.id.activity_main_image);
		try {
			// 獲得AssetManager對象
			assets = getAssets();
			// 擷取/assets目錄下的所有檔案
			images = assets.list("");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 擷取按鈕
		final Button next = (Button) findViewById(R.id.activity_main_btn);
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 如果發生數組越界
				if (currentImg >= images.length) {
					currentImg = 0;
				}
				// 如果目前檔案不是圖檔則找到下一個圖檔檔案
				while (!images[currentImg].endsWith(".png")
						&& !images[currentImg].endsWith(".jpg")
						&& !images[currentImg].endsWith(".gif")) {
					currentImg++;
					// 如果數組已經發生越界
					if (currentImg >= images.length) {
						currentImg = 0;
					}
				}
				InputStream assetFile = null;
				try {
					// 打開指定資源對應的輸入流
					assetFile = assets.open(images[currentImg++]);
				} catch (IOException e) {
					e.printStackTrace();
				}
				BitmapDrawable bitmapDrawable = (BitmapDrawable) image
						.getDrawable();
				// 如果圖檔未回收,先強制回收該圖檔
				if (bitmapDrawable != null
						&& !bitmapDrawable.getBitmap().isRecycled()) {
					bitmapDrawable.getBitmap().recycle();
				}
				// 改變ImageView顯示的圖檔
				image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
			}
		});
	}
}
           

布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <Button
        android:id="@+id/activity_main_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一張" />

    <ImageView
        android:id="@+id/activity_main_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>