Android Drawable和Bitmap的轉(zhuǎn)換實(shí)例詳解
Android Drawable和Bitmap的轉(zhuǎn)換實(shí)例詳解
通常我們需要通過(guò)代碼去設(shè)置圖片,就需要設(shè)置圖片Bitmap和Drawable的轉(zhuǎn)換,下面整理了幾種方式
一、Bitmap轉(zhuǎn)Drawable
Bitmap bm=xxx; //xxx根據(jù)你的情況獲取 BitmapDrawable bd=new BitmapDrawable(bm);//因?yàn)锽timapDrawable是Drawable的子類(lèi),最終直接使用bd對(duì)象即可。
二、 Drawable轉(zhuǎn)Bitmap
Drawable d=xxx; //xxx根據(jù)自己的情況獲取drawable BitmapDrawable bd = (BitmapDrawable) d; Bitmap bm = bd.getBitmap(); //最終bm就是我們需要的Bitmap對(duì)象了。
從資源中獲取Bitmap
public static Bitmap getBitmapFromResources(Activity act, int resId) {
Resources res = act.getResources();
return BitmapFactory.decodeResource(res, resId);
}
byte[] → Bitmap
public static Bitmap convertBytes2Bimap(byte[] b) {
if (b.length == 0) {
return null;
}
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
// Bitmap → byte[]
public static byte[] convertBitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
只是很簡(jiǎn)單代碼片段,還是很容易懂得
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android中一種巧妙的drawable.xml替代方案分享
- Android RippleDrawable 水波紋/漣漪效果的實(shí)現(xiàn)
- Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例
- 淺談Android中Drawable使用知識(shí)總結(jié)
- Android開(kāi)發(fā)基于Drawable實(shí)現(xiàn)圓角矩形的方法
- Android自定義Drawable實(shí)現(xiàn)圓角效果
- Android Bitmap和Drawable的對(duì)比
- Android DrawableTextView圖片文字居中顯示實(shí)例
- Android Drawable必備知識(shí)小結(jié)
- Android drawable微技巧,你不知道的drawable細(xì)節(jié)
相關(guān)文章
Android開(kāi)發(fā)常見(jiàn)錯(cuò)誤小結(jié)
這篇文章主要介紹了Android開(kāi)發(fā)常見(jiàn)錯(cuò)誤,實(shí)例分析了常見(jiàn)的Android開(kāi)發(fā)中遇到的錯(cuò)誤,對(duì)Android開(kāi)發(fā)有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
Android離線Doc文檔訪問(wèn)速度慢的有效解決方法
今天小編就為大家分享一篇關(guān)于Android離線Doc文檔訪問(wèn)速度慢的有效解決方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Android編程開(kāi)發(fā)實(shí)現(xiàn)TextView顯示表情圖像和文字的方法
這篇文章主要介紹了Android編程開(kāi)發(fā)實(shí)現(xiàn)TextView顯示表情圖像和文字的方法,結(jié)合實(shí)例形式分析了Android中TextView的使用技巧,需要的朋友可以參考下2015-12-12
Android使用ContentProvider實(shí)現(xiàn)查看系統(tǒng)短信功能
這篇文章主要為大家詳細(xì)介紹了Android使用ContentProvider實(shí)現(xiàn)查看系統(tǒng)短信功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn)
本篇文章主要介紹了Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Android Secret Code(輸入字符彈出手機(jī)信息)詳解
這篇文章主要介紹了Android Secret Code(輸入字符彈出手機(jī)信息)詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)
本篇文章小編為大家介紹,Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)。需要的朋友參考下2013-04-04

