11.2 Bitmap

簡介

用于圖像處理,可以獲取圖像文件信息,進(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)換

轉(zhuǎn)換

使用

使用
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2021期待與你一起共事,點(diǎn)擊查看崗位[http://m.itdecent.cn/p/6f4d67fa406...
    閑庭閱讀 17,079評論 0 75
  • Bitmap API 1.簡介 Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一,通過bitmap可以獲...
    子墨_guo閱讀 3,364評論 0 7
  • 圖片壓縮就是為了避免我們內(nèi)存溢出,所有要對一系列進(jìn)行壓縮二次采樣等 1.什么是OOM?為什么會引起OOM? out...
    lay_wn閱讀 1,045評論 0 1
  • BitMap壓縮以及二次采樣 標(biāo)簽: Android 首先我們來了解一下什么是oom? 1.什么是OOM?為什么會...
    Zane_Samuel閱讀 1,548評論 1 3
  • 情之所以可貴,在于真、深、純,愛之所以可歌,在于暖、潤、親,戀之所以可敬,在于美、妙、靈。一段連接,一個(gè)刻度,一份...
    木心幽蘭閱讀 186評論 0 0

友情鏈接更多精彩內(nèi)容