開發應用過程中圖片的使用是必不可少的,在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();