溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中Bitmap與Drawable的區(qū)別有哪些

發(fā)布時(shí)間:2020-12-07 15:26:43 來源:億速云 閱讀:419 作者:Leah 欄目:移動(dòng)開發(fā)

Android中Bitmap與Drawable的區(qū)別有哪些?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

Android Bitmap和Drawable的對比

Bitmap - 稱作位圖,一般位圖的文件格式后綴為bmp,當(dāng)然編碼器也有很多如RGB565、RGB888。作為一種逐像素的顯示對象執(zhí)行效率高,但是缺點(diǎn)也很明顯存儲(chǔ)效率低。我們理解為一種存儲(chǔ)對象比較好。

    Drawable - 作為Android平下通用的圖形對象,它可以裝載常用格式的圖像,比如GIF、PNG、JPG,當(dāng)然也支持BMP,當(dāng)然還提供一些高級的可視化對象,比如漸變、圖形等。

A bitmap is a Drawable. A Drawable is not necessarily a bitmap. Like all thumbs are fingers but not all fingers are thumbs.
Bitmap是Drawable . Drawable不一定是Bitmap .就像拇指是指頭,但不是所有的指頭都是拇指一樣.

The API dictates: API規(guī)定:

Though usually not visible to the application, Drawables may take a variety of forms: 盡管通常情況下對于應(yīng)用是不可見的,Drawables 可以采取很多形式:

Bitmap: the simplest Drawable, a PNG or JPEG image. Bitmap: 簡單化的Drawable, PNG 或JPEG圖像. 
Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
States: a compound drawable that selects one of a set of drawables based on its state.
Levels: a compound drawable that selects one of a set of drawables based on its level.
Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.
對比項(xiàng)顯示清晰度支持透明色支持色相色差調(diào)整支持像素操作
Bitmap相同
Drawable相同

Drawable在內(nèi)存占用和繪制速度這兩個(gè)非常關(guān)鍵的點(diǎn)上勝過Bitmap

- Drawable和Bitmap之間可以互相轉(zhuǎn)換,Drawable占用內(nèi)存遠(yuǎn)小于Bitmap。

- setImageDrawable使用資源文件;setImageBitmap使用bitmap圖片,該圖片可能是讀取本地相冊,或者從資源文件轉(zhuǎn)換而來。

- setImageResource()和setImageBitmap()

//setImageResource()
public void setImageResource (int resId)//占用UI thread;
// setImageBitmap()
ImageView iv;
String fileName = "/data/data/com.test/aa.png";
Bitmap bm = BitmapFactory.decodeFile(fileName); 
iv.setImageBitmap(bm); //占用內(nèi)存
// setImageBitmap()
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(image);
// Bitmap轉(zhuǎn)換成Drawable
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(image);
imageView.setImageDrawable(bitmapDrawable);
// 結(jié)論:Bitmap是Drawable . Drawable不一定是Bitmap

關(guān)于Android中Bitmap與Drawable的區(qū)別有哪些問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI