簡介
用于圖像處理,可以獲取圖像文件信息,進(jìn)行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件。
BitmapFactory
Bitmap構(gòu)造是私有的,需要通過工廠類來實(shí)例化。
從文件讀取圖片:public static Bitmap decodeFile(String pathName)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public static Bitmap decodeFile(String pathName, Options opts)
????????eg:Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/bitmap.png");
從輸入流讀取圖片:不會對所加載的圖片進(jìn)行縮放,相當(dāng)從resourse占用內(nèi)存小,效率高
????????????????????????????????public static Bitmap decodeStream(InputStream is)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts)
????????eg:InputStream is = mContext.getAssets().open("bitmap.png"); //本地或網(wǎng)絡(luò)
????????????????bitmap = BitmapFactory.decodeStream(is);
從資源文件讀取圖片:加載的圖片可能會經(jīng)過縮放,消耗內(nèi)存,大量使用容易OOM。(可將resources轉(zhuǎn)成流,使用流讀?。?/p>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 適用場景:性能要求不高,需要圖片自適應(yīng)情況
????????????????????????????????????public static Bitmap decodeResource(Resources res, int id)
? ??????????????????????????????????public static Bitmap decodeResource(Resources res, int id, Options opts)
????????eg:Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),R.raw.bitmap);
從數(shù)組讀取圖片:public static Bitmap decodeByteArray(byte[] data, int offset, int length)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts)
從文件讀取文件 :與decodeFile不同的是這個(gè)直接調(diào)用JNI函數(shù)進(jìn)行讀取,效率比較高。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public static Bitmap decodeFileDescriptor(FileDescriptor fd)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)
注:除了使用BitmapFactory從資源獲取位圖,還可以使用BitmapDrawable對象獲取位圖
? ? ? ? ( new?BitmapDrawable(XXXX).getBitmap )
Option參數(shù)
boolean inJustDecodeBounds:設(shè)置為true,不獲取圖片,不分配內(nèi)存,只返回圖片的高度寬度信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (適用于只想知道bitmap尺寸,不想將其加載到內(nèi)存)
int inSampleSize:圖片縮放的倍數(shù)
? ? ? ? ? ? ? ? 采樣率:1.一般是2的指數(shù)1、2、4、8、16....;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2.設(shè)置小于1會被當(dāng)做1處理,1為原始大?。?/p>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3.若采樣率為2,寬和高為原先1/2,像素和占用內(nèi)存為原來1/4;
int outWidth:獲取圖片的寬度值
int outHeight:獲取圖片的高度值
int inDensity:用于位圖的像素壓縮比
int inTargetDensity:用于目標(biāo)位圖的像素壓縮比(要生成的位圖)
byte[] inTempStorage:創(chuàng)建臨時(shí)文件,將圖片存儲
boolean inScaled:設(shè)置為true時(shí)進(jìn)行圖片壓縮,從inDensity到inTargetDensity
boolean inDither:如果為true,解碼器嘗試抖動解碼(采用隨機(jī)噪聲色來填充,優(yōu)化低位圖色帶明顯的問題)
Bitmap.Config inPreferredConfig:設(shè)置解碼器
String outMimeType:設(shè)置解碼圖像
boolean inPurgeable:當(dāng)存儲Pixel的內(nèi)存空間在系統(tǒng)內(nèi)存不足時(shí)是否可以被回收
boolean inInputShareable:inPurgeable為true情況下才生效,是否可以共享一個(gè)InputStream
boolean inPreferQualityOverSpeed:為true則優(yōu)先保證Bitmap質(zhì)量其次是解碼速度
boolean inMutable:配置Bitmap是否可以更改,比如:在Bitmap上隔幾個(gè)像素加一條線段
inScreenDensity:當(dāng)前屏幕的像素密度
Bitmap.Config inPreferredConfig
Bitmap.Config ARGB_4444:代表 16位 ARGB位圖 ,每個(gè)像素占用 2byte 內(nèi)存(glide默認(rèn)使用這個(gè))
Bitmap.Config ARGB_8888:代表 32位 ARGB位圖,每個(gè)像素占用 4byte 內(nèi)存(想提升質(zhì)量可使用這個(gè))
Bitmap.Config ALPHA_8:代表 8位 Alpha位圖,每個(gè)像素占用 1byte 內(nèi)存(只有透明度沒有顏色值)()
Bitmap.Config RGB_565:代表 8位 RGB位圖,每個(gè)像素占用 2byte 內(nèi)存(沒有透明色,會導(dǎo)致每張圖都會有一個(gè)底)
注:1.位圖位數(shù)越高代表其可以存儲的顏色信息越多,圖像越逼真,占用內(nèi)存越大;
? ? ? ?2.Android中一張圖片(BitMap)占用的內(nèi)存 = 圖片長度 * 圖片寬度 * 單位像素占用的字節(jié)數(shù);
常用方法
void recycle() :回收位圖占用的內(nèi)存空間,把位圖標(biāo)記為Dead
boolean isRecycled():判斷位圖內(nèi)存是否已釋放
int getWidth():獲取位圖的寬度
int getHeight():獲取位圖的高度
boolean isMutable():圖片是否可修改
int getScaledWidth(Canvas canvas):獲取指定密度轉(zhuǎn)換后的圖像的寬度
int getScaledHeight(Canvas canvas):獲取指定密度轉(zhuǎn)換后的圖像的高度
boolean compress(format,quality, stream):按指定的圖片格式以及畫質(zhì),將圖片轉(zhuǎn)換為輸出流
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?format:壓縮圖像的格式,如Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?quality:畫質(zhì)0-100,0表示最低畫質(zhì)壓縮,100以最高畫質(zhì)壓縮(對于PNG等無損格式的圖片,會忽略此項(xiàng)設(shè)置)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?stream: OutputStream中寫入壓縮數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return: 是否成功壓縮到指定的流
static Bitmap createBitmap(Bitmap src):以src為原圖生成不可變得新圖像
static Bitmap createScaledBitmap(...):以src為原圖,指定高、寬、是否可變,創(chuàng)建新的圖像
static Bitmap createBitmap(int width, int height, Config config):創(chuàng)建指定格式、大小的位圖
static Bitmap createBitmap(...) //以source為原圖,指定起始坐標(biāo)xy、寬、高,創(chuàng)建新的圖像
Bitmap與其他格式之間轉(zhuǎn)換

使用
