Android开发实用技巧:Drawable和Bitmap之间的转换
2014-07-24 15:02
621 查看
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。
Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
一. Bitmap转Drawable
Bitmap bm = xxx; //xxx根据你的情况获取
BitmapDrawable bd=new BitmapDrawable(bm);
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
二. Drawable转Bitmap
转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保存成为jpg和png的文件。
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();
}
Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
一. Bitmap转Drawable
Bitmap bm = xxx; //xxx根据你的情况获取
BitmapDrawable bd=new BitmapDrawable(bm);
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
二. Drawable转Bitmap
转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保存成为jpg和png的文件。
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开发之Bitmap 、byte[] 、 Drawable之间的相互转换
- android开发中Bitmap与Drawable之间的相互转换
- Android Bitmap 与 Drawable之间的转换
- Android Drawable、Bitmap、byte[]之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- android在处理图片资源的时候,Drawable、Bitmap、byte[]之间的转换
- Android中Drawable Bitmap Canvas Paint之间区别与转换
- Android Drawable和Bitmap图片之间转换
- android --- Drawable、Bitmap、byte[]之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Drawable、Bitmap、byte[]之间的转换 (android转)
- Bitmap与Drawable之间的转换(Android)
- 【转】Android Bitmap 与 Drawable之间的转换
- Android学习 之 Bitmap Drawable byte[] 三者之间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
- android bitmap drawable 之间进行转换
- Android Bitmap 与 Drawable之间的转换
- Android Drawable和Bitmap图片之间转换
- (转)Android Bitmap 与 Drawable之间的转换
- android中的Bitmap,Drawable,Canvas,Paint之间的区别和转换
- Android中Drawable和Bitmap 之间转换