Android Drawable、Bitmap、byte[]之间的转换
转自http://blog.csdn.net/june5253/article/details/7826597
1、Bitmap-->Drawable
Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
}
2、从资源中获取的Drawable-->Bitmap
Bitmap bitmap=BitmapFactory.decodeResource(res,R.drawable.pic);
3、Bitmap-->Drawable
Drawable bitmap2Drawable(Bitmap bitmap){
new BitmapDrawable(bitmap);
}
4、Bitmap-->byte[]
byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
5、byte[]-->Bitmap
Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
Android Drawable、Bitmap、byte[]之间的转换的更多相关文章
- Stream 和 byte[] 之间的转换
Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...
- C#实现Stream与byte[]之间的转换实例教程
一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...
- C# Stream 和 byte[] 之间的转换
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...
- C# Stream 和 byte[] 之间的转换(文件流的应用)
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...
- Drawable、Bitmap、byte[]之间的转换
android在处理一写图片资源的时候,会进行一些类型的转换: 1 Drawable → Bitmap 的简单方法 ((BitmapDrawable)res.getDrawable(R.drawabl ...
- Android中Bitmap,byte[],Drawable相互转化
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- 【Android】[转] Android中Bitmap,byte[],Drawable相互转化
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- Android -- Drawable && Bitmap
Bitmap转Drawable Bitmap bm=xxx; BitmapDrawable bd=new BitmapDrawable(bm); 因为BtimapDrawable是Drawable的子 ...
- 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)
static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...
- C#下载文件,Stream 和 byte[] 之间的转换
stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...
随机推荐
- javascript--Math相关
最值:Math.max(1,2,3,5);//5 Math.min(1,2,3,5);//1 数组最值:var a=[1,2,3,5]; Math.max.apply(null, a);//最大值 M ...
- bug: 在缓存行高时,总是记录错误.
一,现象: 1.在 cell 中添加了一个 label, 并对 label 设置了 attributeText, 结果滑动的过程中,cell 的高度总是不对,多次出现下一个 cell 覆盖上一个 ce ...
- 浅谈MySql的存储引擎(表类型)
来源:http://www.cnblogs.com/lina1006/archive/2011/04/29/2032894.html 什么是MySql数据库 通常意义上,数据库也就是数据的集合,具体到 ...
- node-webkit安装及简单实现
遇到一个客户说不要登录网页访问系统,说是不安全,要做成像是QQ这样的客户端. 这让我很为难啊,项目都快做好了,不可能让我重新做吧,再说C++什么的我也不会啊, 于是我接触了node-webkit,并觉 ...
- jQuery整理
近几日总是在用js写一些东西,jq用的反而少了,最近在工作中总是会用到不常用的jQuery方法,之前觉得可能用到的情况比较少,便没在意这些方法,结果吃了亏,现在准备重新总结一些jQuery中的一些常用 ...
- Daily Scrum02 12.15
今天会议主要还是大家汇报进度与任务.由于团队中有两位成员在周一会有其他事情处理,暂不分配任务,因而这些事情要交给其他成员处理…… 由于要反复修改,查看效果,所以要花费很长的时间,但大家最近都很忙,我们 ...
- 对Java数组中去除重复项程序分析
我作为一个Java菜鸟,只会用简单的办法来处理这个问题.如果有大神看到,请略过,感激不尽! 所以首先先分析这道题目:数组中重复的数据进行删除,并且要让数组里的数据按原来的顺序排列,中间不能留空. 既然 ...
- html websocket
from:http://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/ websocket 规范升级过,在该链接的文章内未提及,后面 ...
- 如何将Eclipse中的项目迁移到Android Studio 中
如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...
- js测试题
(function(){ return typeof arguments;})();"object" var f = function g(){ return 23; };type ...