Drawable、Bitmap、byte[]之间的转换
android在处理一写图片资源的时候,会进行一些类型的转换:
1 Drawable → Bitmap 的简单方法
((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();
2 Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(rawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
3 Bitmap→Drawable的简单方法
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;
Drawable drawable = (Drawable)bitmapDrawable; Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap);
4 从资源中获取Bitmap
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pic);
5 Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
6 byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b) {
if (b.length!=0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
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( ...
- 将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 ...
- Android Drawable、Bitmap、byte[]之间的转换
转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...
- Image与byte[]之间的转换
//将image转化为二进制 public static byte[] GetByteImage(Image img) { byte[] bt = null; if (!img.Equals(null ...
- C#--整型与字节数组byte[]之间的转换
using System; int i = 123;byte [] intBuff = BitConverter.GetBytes(i); // 将 int 转换成字节数组lob.Write ...
随机推荐
- jmeter linux使用经验小结
1. 确认务必安装了JDK,并且把路径配置OK.否则执行会报错. 2. 当做负载机时,在hosts 配置上 你的ip 你的hostname 或者使用./bin/jmeter-server ...
- RTMP
实时消息传输协议 RTMP(Real Time Messaging Protocol) http://blog.csdn.net/defonds/article/details/17403225 译序 ...
- Linux下删除大量文件
主要参考了http://www.slashroot.in/which-is-the-fastest-method-to-delete-files-in-linux 首先建立50万个文件 ➜ test ...
- Oracle系列之索引
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 Oracle索引.权限 介绍 为什么添加了索引后,会加快查询速度呢? 索引是用于加速数据存取的数据对象.合理的使用索引可以大大降低i/ ...
- Linux改变进程优先级的nice命令
前言: VPS普遍性能不高,很多人可能有这样一个感受,在执行du.tar等命令时,会造成系统负载飙升,Apache响应缓慢.这时nice命令改变进程优先级可能能缓解这种状况.nice命令用于调整Lin ...
- BZOJ_1009_[HNOI2008]_GT考试_(动态规划+kmp+矩阵乘法优化+快速幂)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串全部由0~9组成,给出一个串s,求一个长度为n的串,不包含s的种类有多少. 分析 ...
- Struts 2.3.1.1 命令执行漏洞
漏洞版本: Struts 2.3.1.1 漏洞描述: CVE ID:CVE-2011-3923 Struts2的核心使用的是WebWork框架,而WebWork通过XWork来处理用户的请求参数.Xw ...
- [Tommas] UNION 和 UNION ALL 的区别
UNION指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看,UNION跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料.UNION的一个限制是两个 SQL 语句所产生的 ...
- SQL Server查询性能优化——创建索引原则(一)
索引是什么?索引是提高查询性能的一个重要工具,索引就是把查询语句所需要的少量数据添加到索引分页中,这样访问数据时只要访问少数索引的分页 就可以.但是索引对于提高查询性能也不是万能的,也不是建立越多的索 ...
- 关于 Unity 中 ModelImporter.optimizeGameObjects
ModelImporter.optimizeGameObjects 能够优化骨骼动画,将无用的骨骼合并,效率测试官方给出的数据差距比较大( Unity CJ 干货分享:全新的Unity移动游 ...