1. 程式人生 > >Android突破一之Bitmap、BitmapFactory、BitmapDrawable類

Android突破一之Bitmap、BitmapFactory、BitmapDrawable類

Bitmap、BitmapFactory、BitmapDrawable Jiangdg_VIP
http://blog.csdn.net/u012637501

一、概要:     點陣圖由圖形影象及其屬性的畫素資料組成。Bitmap 是 Bitmap是Android系統中的影象處理的最重要類一,用於處理由畫素資料定義的影象的物件。用它可以獲取影象檔案資訊,進行影象剪下、旋轉、縮放等操作,並可以指定格式儲存影象檔案。 二、特點:public final class即不能被繼承; ava.lang.Object    ↳ android.graphics.Bitmap 三、類方法 1.Bitmap類
public void recycle()——回收點陣圖佔用的記憶體空間,把點陣圖標記為Dead public final boolean isRecycled() ——判斷點陣圖記憶體是否已釋放 public final int getWidth()——獲取點陣圖的寬度 public final int getHeight()——獲取點陣圖的高度 public final boolean isMutable()——圖片是否可修改 public int getScaledWidth(Canvas canvas)——獲取指定密度轉換後的影象的寬度 public int getScaledHeight(Canvas canvas)——獲取指定密度轉換後的影象的高度
public boolean compress(CompressFormat format, int quality, OutputStream stream)——按指定的圖片格式以及畫質,將圖片轉換為輸出流。 format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality:畫質,0-100.0表示最低畫質壓縮,100以最高畫質壓縮。對於PNG等無損格式的圖片,會忽略此項設定。  2.BitmapFactory工廠類  Option 引數類: public boolean inJustDecodeBounds——如果設定為true,不獲取圖片,不分配記憶體,但會返回圖片的高度寬度資訊。
public int inSampleSize——圖片縮放的倍數。如果設為4,則寬和高都為原來的1/4,則圖是原來的1/16。 public int outWidth——獲取圖片的寬度值 public int outHeight——獲取圖片的高度值 public int inDensity——用於點陣圖的畫素壓縮比 public int inTargetDensity——用於目標點陣圖的畫素壓縮比(要生成的點陣圖) public boolean inScaled——設定為true時進行圖片壓縮,從inDensity到inTargetDensity。 讀取一個檔案路徑得到一個位圖。如果指定檔案為空或者不能解碼成檔案,則返回NULL。 public static Bitmap decodeFile(String pathName, Options opts) public static Bitmap decodeFile(String pathName) 讀取一個資原始檔得到一個位圖。如果點陣圖資料不能被解碼,或者opts引數只請求大小資訊時,則返回NuLL。  (即當Options.inJustDecodeBounds=true,只請求圖片的大小資訊。) public static Bitmap decodeResource(Resources res, int id) public static Bitmap decodeResource(Resources res, int id, Options opts) 從輸入流中解碼點陣圖   public static Bitmap decodeStream(InputStream is) 從位元組陣列中解碼生成不可變的點陣圖 public static Bitmap decodeByteArray(byte[] data, int offset, int length) 3.BitmapDrawable類:繼承於Drawable,包裝了一個位圖,這個點陣圖可以被平鋪、拉伸、對齊。你可以從檔案路徑、輸入流、XML檔案以及Bitmap中建立。 如果是在XML資源中建立,則需要<bitmap>元素。 常用的建構函式: Resources res=getResources();//獲取資源 public BitmapDrawable(Resources res)——建立一個空的drawable。(Response用來指定初始時所用的畫素密度)替代public BitmapDrawable()方法(此方法不處理畫素密度) public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap. public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream. final Bitmap getBitmap():返回被這個drawable渲染的點陣圖 四、使用詳解:     按照對點陣圖的操作,分為以下幾個功能分別介紹: 從資源中獲取點陣圖、獲取點陣圖的資訊、顯示點陣圖、點陣圖縮放、點陣圖旋轉。  1.從資源中獲取點陣圖:可以使用BitmapDrawable或者BitmapFactory來獲取資源中的點陣圖。 a.使用BitmapDrawable獲取點陣圖  步驟:首先獲取資原始檔,然後使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;再使用BitmapDrawable類的getBitmap()獲取得到點陣圖。另外,BitmapDrawable提供了顯示點陣圖等操作。 
   Resources res=getResources();                                         //獲取資原始檔
     InputStream is=res.openRawResource(R.drawable.plane); // 讀取資原始檔獲取輸入流
     BitmapDrawable bmpDraw=new BitmapDrawable(is);       //例項化一個BitmapDrawable物件
     Bitmap bmp=bmpDraw.getBitmap();                                //使用BitmapDrawable類的getBitmap()獲取得到點陣圖
或者:
      BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.plane); //獲取並讀取資原始檔
      Bitmap bmp=bmpDraw.getBitmap();                                //使用getBitmap()方法獲取得到點陣圖,返回值為Bitmap物件
b.使用BitmapFactory 獲取點陣圖 步驟:兩種方式,一是使用BitmapFactory類decodeStream(InputStream is)解碼點陣圖資源,獲取點陣圖。二是使用BitmapFactory類Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.plane); 方法解碼點陣圖資源。由於BitmapFactory的所有函式都是static,所有的函式直接由類呼叫,這個輔助類可以通過資源ID、路徑、檔案、資料流等方式來獲取點陣圖。
    Resources res=getResources();                                                             //獲取資原始檔
    Bitmap plane=BitmapFactory.decodeResource(res, R.drawable.plane); //獲取圖片
注意,res也可以使用context.getResources()替代。在Android SDK中說明可以支援的圖片格式如下:png (preferred), jpg (acceptable), gif (discouraged),雖然bmp格式沒有明確說明,但是在Android SDK Support Media Format中是明確說明了。 2. 獲取點陣圖的資訊     要獲取點陣圖資訊,比如點陣圖大小是否包含透明度顏色格式等,獲取得到Bitmap就迎刃而解了,這些資訊在Bitmap的函式中可以輕鬆獲取到。需要注意的是在Bitmap中對RGB顏色格式使用Bitmap.Config定義,僅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如說RGB_555,在開發中可能需要。 3. 顯示點陣圖     顯示點陣圖可以使用核心類Canvas,通過Canvas類的drawBirmap()顯示點陣圖,或者藉助於BitmapDrawable來將Bitmap繪製到Canvas。也可以通過BitmapDrawable將點陣圖顯示到View中, 轉換為BitmapDrawable物件顯示點陣圖。
方法一: 通過BitmapDrawable將點陣圖顯示到View中, 轉換為BitmapDrawable物件顯示點陣圖
        Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);  // 獲取點陣圖 
        BitmapDrawable bmpDraw=new BitmapDrawable(bmp);    // 轉換為BitmapDrawable物件 
        ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);  // 顯示點陣圖
         iv2.setImageDrawable(bmpDraw); 
方法二:通過Canvas類drawBirmap()顯示點陣圖 -採用一個繼承自View的子類Panel,在子類的onDraw中顯示
    public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Panel(this));        //將點陣圖顯示在Activity上
    }   
    class Panel extends View{
        public Panel(Context context) {
            super(context);
        }
        public void onDraw(Canvas canvas){
             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);    //獲取點陣圖
             canvas.drawColor(Color.BLACK);                                                                               
             canvas.drawBitmap(bmp, 10, 10, null);                                                                           //顯示點陣圖
        }
    }
} 
4. 點陣圖縮放 (1)將一個位圖按照需求重畫一遍,畫後的點陣圖就是我們需要的了,與點陣圖的顯示幾乎一樣:         drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。 (2)在原有點陣圖的基礎上,縮放原點陣圖,建立一個新的點陣圖:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) (3)藉助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不過要注意此時整個畫布都縮放了。 (4)藉助Matrix: 
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);    //獲取點陣圖
Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true);    
canvas.drawColor(Color.BLACK);                                                                             //顯示點陣圖
canvas.drawBitmap(dstbmp, 10, 10, null);                            <span style="font-family: 'Times New Roman'; background-color: rgb(255, 255, 255);">   </span>
5. 點陣圖旋轉     同樣,點陣圖的旋轉也可以藉助Matrix或者Canvas來實現。Matrix線上性代數中都學習過,Android SDK提供了Matrix類,可以通過各種介面來設定矩陣。結合上面的例子程式,將點陣圖縮放例子程式在顯示點陣圖的時候前,增加點陣圖旋轉功能,修改程式碼如下:    
Matrix matrix = new Matrix();
//matrix.postScale(0.5f, 0.5f);
matrix.setRotate(90,120,130);
canvas.drawBitmap(mbmpTest, matrix, mPaint);     
                      參考資料: