ImageView 四個(gè)指定圖片的方法區(qū)別

今天在看人家一個(gè)繼承ImagView的自定義控件庸毫,發(fā)現(xiàn)他覆寫(xiě)了這下面這四個(gè)方法。就在網(wǎng)上找了下這四個(gè)方法有啥區(qū)別,最后在Google Plus上找到了下面的解釋竖般。英文很差的我都看懂了,所以就不翻譯了茶鹃,直接搬運(yùn)過(guò)來(lái)了涣雕。

ImageView has 4 APIs to specify the image. Which one to use? What is the difference?

  1. setImageDrawable(Drawable drawable)
  2. setImageBitmap(Bitmap bm)
  3. setImageResource(int resId)
  4. setImageURI(URI uri)

ImageView, by the name, is used to display an image. But what is a image? A Bitmap is-a image, not hard to understand and we use setImageBitmap for that purpose. However, internally, the ImageView has-a Drawable but not a Bitmap and that is what setImageDrawable for. When you call setImageBitmap, internally, first the bitmap will be wrapped to BitmapDrawable, which IS-A Drawable , and then call setImageDrawable.

Here is the code.

public void setImageBitmap(Bitmap bm) {
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}

So, what about the 3 and 4 API?

You should already know that that are bunches of ways to create a bitmap, from a file path, from the Uri, or from the resource file.

BitmapFactory.decodeFile(String pathName)
BitmapFactory.decodeStream(Inputstream)
BitmapFactory.decodeResource(Resource res, int id)
BitmapFactory.decodeByteArray(byte[] data)

Aware of this, it is easy to understand setImageResource/setImageUri is just same as setImageBitmap.

To sum up, setImageDrawable is the primitive function other APIs rely on. The other 3 are just helper methods making you write less code.

In addition, it is very important to keep in mind that ImageView actually has-a Drawable, which not necessarily to be a BitmapDrawable! You could set any Drawable to the Image view.

Besides setting the Drawable through Java API, you could also using XML attribution to set the source Drawable for ImageView. See example below. Note that the shape could be either an image file (.png,.jpg,.bmp) or xml file.

<ImageView
                           android:layout_width="match_parent"
                           android:layout_height="50dip"
                           android:src="@drawable/shape"/>

shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="270"/>
   <padding android:left="7dp" android:top="7dp android:right="7dp" android:bottom="7dp" />
   <corners android:radius="8dp" />
</shape>

原文鏈接(要梯子):
https://plus.google.com/+PierrChen/posts/VNAfFLDcKrw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末艰亮,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子挣郭,更是在濱河造成了極大的恐慌迄埃,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兑障,死亡現(xiàn)場(chǎng)離奇詭異侄非,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)旺垒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)彩库,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人先蒋,你說(shuō)我怎么就攤上這事骇钦。” “怎么了竞漾?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵眯搭,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我业岁,道長(zhǎng)鳞仙,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任笔时,我火速辦了婚禮棍好,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘允耿。我一直安慰自己借笙,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布较锡。 她就那樣靜靜地躺著业稼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蚂蕴。 梳的紋絲不亂的頭發(fā)上低散,一...
    開(kāi)封第一講書(shū)人閱讀 52,328評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音骡楼,去河邊找鬼熔号。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鸟整,可吹牛的內(nèi)容都是我干的跨嘉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼祠乃!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起兑燥,我...
    開(kāi)封第一講書(shū)人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤亮瓷,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后降瞳,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體嘱支,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年挣饥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了除师。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡扔枫,死狀恐怖汛聚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情短荐,我是刑警寧澤倚舀,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站忍宋,受9級(jí)特大地震影響痕貌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜糠排,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一舵稠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧入宦,春花似錦哺徊、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至汹忠,卻和暖如春淋硝,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宽菜。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工谣膳, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人铅乡。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓继谚,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親阵幸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子花履,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容