Android Drawable和Bitmap的轉(zhuǎn)換實例詳解
Android Drawable和Bitmap的轉(zhuǎn)換實例詳解
通常我們需要通過代碼去設(shè)置圖片,就需要設(shè)置圖片Bitmap和Drawable的轉(zhuǎn)換,下面整理了幾種方式
一、Bitmap轉(zhuǎn)Drawable
Bitmap bm=xxx; //xxx根據(jù)你的情況獲取 BitmapDrawable bd=new BitmapDrawable(bm);//因為BtimapDrawable是Drawable的子類,最終直接使用bd對象即可。
二、 Drawable轉(zhuǎn)Bitmap
Drawable d=xxx; //xxx根據(jù)自己的情況獲取drawable BitmapDrawable bd = (BitmapDrawable) d; Bitmap bm = bd.getBitmap(); //最終bm就是我們需要的Bitmap對象了。
從資源中獲取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();
}
只是很簡單代碼片段,還是很容易懂得
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android中一種巧妙的drawable.xml替代方案分享
- Android RippleDrawable 水波紋/漣漪效果的實現(xiàn)
- Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例
- 淺談Android中Drawable使用知識總結(jié)
- Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法
- Android自定義Drawable實現(xiàn)圓角效果
- Android Bitmap和Drawable的對比
- Android DrawableTextView圖片文字居中顯示實例
- Android Drawable必備知識小結(jié)
- Android drawable微技巧,你不知道的drawable細(xì)節(jié)
相關(guān)文章
Android編程開發(fā)實現(xiàn)TextView顯示表情圖像和文字的方法
這篇文章主要介紹了Android編程開發(fā)實現(xiàn)TextView顯示表情圖像和文字的方法,結(jié)合實例形式分析了Android中TextView的使用技巧,需要的朋友可以參考下2015-12-12
Android使用ContentProvider實現(xiàn)查看系統(tǒng)短信功能
這篇文章主要為大家詳細(xì)介紹了Android使用ContentProvider實現(xiàn)查看系統(tǒng)短信功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Android Secret Code(輸入字符彈出手機信息)詳解
這篇文章主要介紹了Android Secret Code(輸入字符彈出手機信息)詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android SlidingDrawer 抽屜效果的實現(xiàn)
本篇文章小編為大家介紹,Android SlidingDrawer 抽屜效果的實現(xiàn)。需要的朋友參考下2013-04-04

