android圖像與圖像處理系列(一、Bitmap和BitmapFactory)

1、Drawable對象

  Android應用添加了Drawabe資源之後,Android SDK會爲這份資源文件在R清單文件中創建一個索引項:R.drawable.file_name,接着我們可以在xml資源文件中通過@drawable/file_name來訪問該drawable對象,也可以在java代碼中通過R.drawable.file_name來訪問該drawable對象,在java代碼中R.drawable.file_name只是一個int類型的常量,它只代表了drawable對象的一個id,如果要獲取實際的drawable對象,則需要調用Resources的getDrawable(int id)方法來獲取。

2、Bitmap與BitmapFactory

    (1)Bitmap代表一張位圖,BitmapDrawable裏封裝的圖片就是一個Bitmap對象,開發者爲了把一個Bitmap對象封裝成BitmapDrawable對象,可以調用BitmapDrawable的構造器:
      把一個Bitmap對象包裝成BitmapDrawable對象
      BitmapDrawable drawable = new BitmapDrawable(bitmap);
    (2)如果需要獲取BitmapDrawable所包裝的Bitmap對象,可以調用BitmapDrawable的getBitmap()方法:
        獲取BitmapDrawable對象所包裝的Bitmap對象
        Bitmap bitmap = drawable.getBitmap();
   (3)Bitmap提供了一些靜態方法來創建新的Bitmap對象: 
        Bitmap.createBitmap(source, x, y, width,height):從源位圖source的指定座標點x,y開始,從中挖取寬width、高height的一塊區域,創建新的Bitmap
      Bitmap.createScaledBitmap(src, dstWidth, dstHeight,filter):對源位圖src進行縮放,縮成寬dstWidth、高DSTHeight的新位圖 
        Bitmap.createBitmap(width,height, config):創建一個寬width、高height的新位圖 
        Bitmap.createBitmap(source, x, y,width, height, m,filter):從源位圖source的指定座標點x,y開始,從中挖取寬width、高height的一塊區域,創建新的Bitmap
          ,並按照Matrix指定的規則進行變換
  (4)BitmapFactory是一個工具類,他提供了一些方法用於從不同的數據源來解析、創建Bitmap對象:
       BitmapFactory.decodeByteArray(byte[] data, int offset, int length):從指定字節數組的offset位置開始,將長度爲length的字節數據解析成Bitmap對象
      BitmapFactory.decodeFile(String pathName):從pathName指定的文件中解析、創建Bitmap對象
      BitmapFactory.decodeFileDescriptor(FileDescriptor fd):用於從FileDescriptor對應的文件中解析、創建Bitmap對象
      BitmapFactory.decodeResource(Resources res, int id):用於根據給定的資源ID從指定資源文件中解析、創建Bitmap對象 
        BitmapFactory.decodeStream(InputStream is):用於從指定輸入流中解析、創建Bitmap對象
  (5)Android爲Bitmap提供了兩個方法來判斷它是否已經回收,以及強制Bitmap回收自己
      isRecycled():判斷Bitmap對象是否已經回收
       recycle():強制一個Bitmap對象立即回收自己

3、實例:圖片查看器,查詢assets目錄下的圖片

佈局文件:

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

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="300dp"
        android:layout_height="300dp" />

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="搜索下一個" />

</LinearLayout>

activity文件:

package com.example.image;

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;

/**
 * Bitmap和BitmapFactory
 * @author yinbenyang
 */
public class MainActivity extends Activity {

    private ImageView imageview;
    String[] images = null;
    private Button btn;
    // 用於管理assets文件夾下的資源
    AssetManager assets = null;
    // 當前圖片
    int currentImg = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageview = (ImageView) findViewById(R.id.imageview);
        btn = (Button) findViewById(R.id.btn);
        assets = getAssets();
        try {
            // 獲取/assets目錄下的所有文件
            images = assets.list("");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 點擊按鈕查看下一張圖片
        btn.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 bitDrawable = (BitmapDrawable)imageview.getDrawable();
                //如果圖片沒有回收,先強制回收該圖片
                if(bitDrawable != null && !bitDrawable.getBitmap().isRecycled()){
                    bitDrawable.getBitmap().recycle();
                }
                //改變ImageView顯示的圖片
                imageview.setImageBitmap(BitmapFactory.decodeStream(assetFile));
            }
        });
    }
}

實例效果如下:點擊搜索下一個,輪換顯示圖片

發佈了98 篇原創文章 · 獲贊 20 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章