Android Drawable和Bitmap的转换实例详解
更新时间:2017年05月17日 08:38:22 投稿:lqh
这篇文章主要介绍了Android Drawable和Bitmap的转换实例详解的相关资料,需要的朋友可以参考下
Android Drawable和Bitmap的转换实例详解
通常我们需要通过代码去设置图片,就需要设置图片Bitmap和Drawable的转换,下面整理了几种方式
一、Bitmap转Drawable
Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd=new BitmapDrawable(bm);//因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
二、 Drawable转Bitmap
Drawable d=xxx; //xxx根据自己的情况获取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编程开发实现TextView显示表情图像和文字的方法
这篇文章主要介绍了Android编程开发实现TextView显示表情图像和文字的方法,结合实例形式分析了Android中TextView的使用技巧,需要的朋友可以参考下2015-12-12
Android使用ContentProvider实现查看系统短信功能
这篇文章主要为大家详细介绍了Android使用ContentProvider实现查看系统短信功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-11-11
Android Secret Code(输入字符弹出手机信息)详解
这篇文章主要介绍了Android Secret Code(输入字符弹出手机信息)详解的相关资料,需要的朋友可以参考下2016-11-11


最新评论