一、Bitmap轉Drawable
Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp);
因為BtimapDrawable是Drawable的子類,最終直接使用bd對象即可。
二、 Drawable轉Bitmap
轉成Bitmap對象后,可以將Drawable對象通過Android的SK庫存成一個字節輸出流,最終還可以保存成為jpg和png的文件。
Drawable d=xxx; BitmapDrawable bd = (BitmapDrawable) d; Bitmap bm = bd.getBitmap();
最終bm就是我們需要的Bitmap對象了。
Drawable->Bitmap
public static Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
三、Bitmap轉byte[]
public static byte[] convertBitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
四、BitmapFactory圖片解析工具類
BitmapFactory自帶了一套decodeXXX(params...)方法,用於解析獲取圖片;其中一個需要注意的細節是opts參數,因為比較常見的是大圖,或者一系列的圖片,而過多的圖片就會有內存溢出的潛在可能性,這個opts參數即是用於設置解析圖片流的一些參數的,比如設置寬高,圖片縮放等等;
下面是google官方關於BitmapFactory.Options的字段的解釋;
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
public Bitmap | inBitmap | If set, decode methods that take the Options object will attempt to reuse this bitmap when loading content. | |||||||||
public int | inDensity | The pixel density to use for the bitmap. | |||||||||
public boolean | inDither | If dither is true, the decoder will attempt to dither the decoded image. | |||||||||
public boolean | inInputShareable | This field works in conjuction with inPurgeable. | |||||||||
public boolean | inJustDecodeBounds | If set to true, the decoder will return null (no bitmap), but the out... | |||||||||
public boolean | inMutable | If set, decode methods will always return a mutable Bitmap instead of an immutable one. | |||||||||
public boolean | inPreferQualityOverSpeed | If inPreferQualityOverSpeed is set to true, the decoder will try to decode the reconstructed image to a higher quality even at the expense of the decoding speed. | |||||||||
public Bitmap.Config | inPreferredConfig | If this is non-null, the decoder will try to decode into this internal configuration. | |||||||||
public boolean | inPremultiplied | If true (which is the default), the resulting bitmap will have its color channels pre-multipled by the alpha channel. | |||||||||
public boolean | inPurgeable | If this is set to true, then the resulting bitmap will allocate its pixels such that they can be purged if the system needs to reclaim memory. | |||||||||
public int | inSampleSize | If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. | |||||||||
public boolean | inScaled | When this flag is set, if inDensity and inTargetDensity are not 0, the bitmap will be scaled to matchinTargetDensity when loaded, rather than relying on the graphics system scaling it each time it is drawn to a Canvas. |
|||||||||
public int | inScreenDensity | The pixel density of the actual screen that is being used. | |||||||||
public int | inTargetDensity | The pixel density of the destination this bitmap will be drawn to. | |||||||||
public byte[] | inTempStorage | Temp storage to use for decoding. | |||||||||
public boolean | mCancel | Flag to indicate that cancel has been called on this object. | |||||||||
public int | outHeight | The resulting height of the bitmap, set independent of the state of inJustDecodeBounds. | |||||||||
public String | outMimeType | If known, this string is set to the mimetype of the decoded image. | |||||||||
public int | outWidth | The resulting width of the bitmap, set independent of the state of inJustDecodeBounds. |
挑幾個通常情況下需要用到的字段
inJustDecodeBounds:boolean類型變量,設置為true時表示只獲取圖片的寬高;而不獲取實際圖片;
outHeight:此參數可以進行設置,值為圖片的高度,可以通過設置inJustDecodeBounds為true獲取圖片的高度,然后再對這個高度進行設置,以獲取理想高度的圖片;
outWidght:獲取圖片寬度,值的含義同上;
inSampleSize:如果設置值大小1,表示獲取一個小一點的圖片,用於節省內存,比如:inSampleSize = 4 返回一個原圖1/4寬高的圖像(圖片像素個數為原圖1/16);
獲取一個大圖的步驟通常如下:
1。傳入一個參數opts,設置opts.inJustDecodeBounds為true;
2。獲取寬高信息,再通過outHeight、outWidth設置理想圖片寬高;
3。重新設置opts.inJustDecodeBounds為false,表示需要獲取圖片;
4。設置inSampleSize,用於節省內存;
有時候可能還需要設置其它的參數,這里列舉兩個
inPreferredConfig = Bitmap.Config.ARGB_4444;默認是Bitmap.Config.ARGB_8888;
inPurgeablein、InputShareable 這兩個參數需要同時使用;