源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android Drawable和Bitmap的转换实例详解

  • 时间:2022-06-22 10:51 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android Drawable和Bitmap的转换实例详解
[b]Android Drawable和Bitmap的转换实例详解[/b] 通常我们需要通过代码去设置图片,就需要设置图片Bitmap和Drawable的转换,下面整理了几种方式 [b]一、Bitmap转Drawable[/b]
Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd=new BitmapDrawable(bm);//因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
[b]二、 Drawable转Bitmap[/b]
Drawable d=xxx; //xxx根据自己的情况获取drawable
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();
//最终bm就是我们需要的Bitmap对象了。
[b]从资源中获取Bitmap[/b]
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();
}
只是很简单代码片段,还是很容易懂得 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部