Android Bitmap

  開發應用過程中圖片的使用是必不可少的,在Android中除了使用Drawable資源中的圖片,我們還可以使用Bitmap,Picture類等創建圖片。
  

Bitmap

  Bitmap代表一張位圖。Bitmap能能夠直接創建,要通過Bitmap.Factory來創建Bitmap的對象。
  
  Bitmap.Factory中爲我們提供了多個方法來獲得Bitmap的對象:

  • decodeByteArray(byte[] data, int offset, int length)
      將制定字節數組從offset字節開始length長度的字節解析成Bitmap對象。
      
  • decodeFile(String pathName)
      將指定路徑下的文件解析成Bitmap對象。
      
  • decodeFileDescriptor(FileDescriptor fd)
      將FileDescriptor對應文件中解析,創建Bitmap對象。
      
  • decodeResource(Resources res, int id)
      將給定的資源ID解析成Bitmap對象。
      
  • decodeStream(InputStream is)
      將指定的字節流解析成Bitmap對象。
      
      除此之外Bitmap還提供了一些靜態的方法創建Bitmap對象:

  • createBitmap(Bitmap source, int x, int y, int width, int height)
      從源位圖的指定座標(x, y)開始,挖取寬度爲width,高度爲height的圖像創建Bitmap對象。
      

  • createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
      將源位圖縮放成寬度爲dstWidth,高度爲dstHeight的Bitmap的對象。
      
  • createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
      從源位圖的(x, y)座標開始,挖取寬度爲width,高度爲height的圖像並按照Matrix的規定設置爲型的Bitmap對象。

Bitmap與BiamapDrawable

  BitmapDrawable中封裝的就是一個Bitmap對象:

BitmapDrawable drawable = new BitmapDrawable (bitmap);

  通過調用BitmapDrawable的getBitmap()方法獲得BitmapDrawable中封裝的Bitmap對象。

Bitmap bitmap= drawable.getBitmap();
發佈了134 篇原創文章 · 獲贊 134 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章