android Bitmap學習總結


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等無損格式的圖片,會忽略此項設置。

常用的靜態方法:
  1. public static Bitmap createBitmap(Bitmap src) ——以src爲原圖生成不可變得新圖像
  2. public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
  3.             int dstHeight, boolean filter)——以src爲原圖,創建新的圖像,指定新圖像的高寬以及是否可變。
  4. public static Bitmap createBitmap(int width, int height, Config config)——創建指定格式、大小的位圖
  5. public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source爲原圖,創建新的圖片,指定起始座標以及新圖像的高寬。
  6. public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
  7.              Matrix m, boolean filter)

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。
  1. public static Bitmap decodeFile(String pathName, Options opts)
  2. public static Bitmap decodeFile(String pathName)

讀取一個資源文件得到一個位圖。如果位圖數據不能被解碼,或者opts參數只請求大小信息時,則返回NuLL。
(即當Options.inJustDecodeBounds=true,只請求圖片的大小信息。)
  1. public static Bitmap decodeResource(Resources res, int id)
  2. public static Bitmap decodeResource(Resources res, int id, Options opts)

從輸入流中解碼位圖
public static Bitmap decodeStream(InputStream is)
從字節數組中解碼生成不可變的位圖
  1. public static Bitmap decodeByteArray(byte[] data, int offset, int length)

BitmapDrawable類:繼承於Drawable,你可以從文件路徑、輸入流、XML文件以及Bitmap中創建。
常用的構造函數:
Resources res=getResources();//獲取資源
public BitmapDrawable(Resources res)——創建一個空的drawable。(Response用來指定初始時所用的像素密度)替代public BitmapDrawable()方法(此方法不處理像素密度)
  1. public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap
  2. public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap.
  3. public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream.

轉:http://blog.csdn.net/xxxx1243/article/details/5834187
首先介紹我們最常用的Bitmap(位圖)。位圖是我們開發中最常用的資源,畢竟一個漂亮的界面對用戶是最有吸引力的。按照對位圖的操作,分爲以下幾個功能分別介紹:
從資源中獲取位圖
獲取位圖的信息
顯示位圖
位圖縮放
位圖旋轉
1. 從資源中獲取位圖
可以使用BitmapDrawable或者BitmapFactory來獲取資源中的位圖。
當然,首先需要獲取資源: Resources res=getResources();
使用BitmapDrawable獲取位圖
   1. 使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;
   2. 使用BitmapDrawable類的getBitmap()獲取得到位圖;
通過Resource的函數:InputStream  openRawResource(int id)獲取得到資源文件的數據流後,也可以通過2種方法來獲取Bitmap,如下:
使用BitmapDrawable
(A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.)
使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;
使用BitmapDrawable類的getBitmap()獲取得到位圖;
BitmapDrawable也提供了顯示位圖等操作。
Java代碼:
  1. InputStream is=res.openRawResource(R.drawable.pic180);  // 讀取資源文件獲取輸入流
  2. BitmapDrawable bmpDraw=new BitmapDrawable(is);   
  3. Bitmap bmp=bmpDraw.getBitmap();
  4. 或者:
  5. BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);   
  6. Bitmap bmp=bmpDraw.getBitmap();

使用BitmapFactory
(Creates Bitmap objects from various sources, including files, streams, and byte-arrays.)
使用BitmapFactory類decodeStream(InputStream is)解碼位圖資源,獲取位圖。
使用BitmapFactory類Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180); 方法解碼位圖資源。
BitmapFactory的所有函數都是static,這個輔助類可以通過資源ID、路徑、文件、數據流等方式來獲取位圖。
以上方法在編程的時候可以自由選擇,在Android SDK中說明可以支持的圖片格式如下:png (preferred), jpg (acceptable), gif (discouraged),雖然bmp格式沒有明確說明,但是在Android SDK Support Media Format中是明確說明了。
代碼:
方法1:

2. 獲取位圖的信息
要獲取位圖信息,比如位圖大小、是否包含透明度、顏色格式等,獲取得到Bitmap就迎刃而解了,這些信息在Bitmap的函數中可以輕鬆獲取到。Android SDK中對Bitmap有詳細說明,閱讀起來也比較容易,不在此詳細說明,這裏只是輔助說明以下2點:
在Bitmap中對RGB顏色格式使用Bitmap.Config定義,僅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如說RGB_555,在開發中可能需要注意這個小問題;
Bitmap還提供了compress()接口來壓縮圖片,不過AndroidSAK只支持PNG、JPG格式的壓縮;其他格式的需要Android開發人員自己補充了。
3. 顯示位圖
顯示位圖可以使用核心類Canvas,通過Canvas類的drawBirmap()顯示位圖,或者藉助於BitmapDrawable來將Bitmap繪製到Canvas。當然,也可以通過BitmapDrawable將位圖顯示到View中。
轉換爲BitmapDrawable對象顯示位圖
  1.      // 獲取位圖
  2.         Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
  3.         // 轉換爲BitmapDrawable對象
  4.         BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
  5.         // 顯示位圖
  6.         ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
  7.        iv2.setImageDrawable(bmpDraw);

使用Canvas類顯示位圖
這兒採用一個繼承自View的子類Panel,在子類的OnDraw中顯示
Java代碼
  1. public class MainActivity extends Activity {   
  2.      
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {   
  5.         super.onCreate(savedInstanceState);   
  6.         setContentView(new Panel(this));   
  7.     }   
  8.       
  9.     class Panel extends View{            
  10.         public Panel(Context context) {     
  11.             super(context);   
  12.         }         
  13.         public void onDraw(Canvas canvas){     
  14.             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);   
  15.              canvas.drawColor(Color.BLACK);     
  16.             canvas.drawBitmap(bmp, 10, 10, null);     
  17.         }     
  18.     }   


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:
Java代碼
  1. Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);   
  2. Matrix matrix=new Matrix();   
  3. matrix.postScale(0.2f, 0.2f);   
  4. Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
  5. bmp.getHeight(),matrix,true);   
  6. canvas.drawColor(Color.BLACK);     
  7. canvas.drawBitmap(dstbmp, 10, 10, null);   

5. 位圖旋轉
同樣,位圖的旋轉也可以藉助Matrix或者Canvas來實現。Matrix在線性代數中都學習過,Android SDK提供了Matrix類,可以通過各種接口來設置矩陣。結合上面的例子程序,將位圖縮放例子程序在顯示位圖的時候前,增加位圖旋轉功能,修改代碼如下:
  1. Matrix matrix = new Matrix();
  2. //matrix.postScale(0.5f, 0.5f);
  3. matrix.setRotate(90,120,130);
  4. canvas.drawBitmap(mbmpTest, matrix, mPaint);

旋轉後的位圖顯示如下:

除了這種方法之外,我們也可以在使用Bitmap提供的函數如下:
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter),在原有位圖旋轉的基礎上,創建新位圖。
總結說明
對位圖的操作,結合Android SDK中的類,詳細的介紹完了。最後還需要強調的是:這篇文章只是對Android SDK中代碼閱讀分析,它代替不了你閱讀Android SDK,深入的學習還是要仔細的閱讀Android SDK。

轉載自:http://blog.sina.com.cn/s/blog_5da93c8f0100w3xz.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章