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[]之间的转换的更多相关文章

  1. Stream 和 byte[] 之间的转换

    Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...

  2. C#实现Stream与byte[]之间的转换实例教程

    一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...

  3. C# Stream 和 byte[] 之间的转换

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  4. C# Stream 和 byte[] 之间的转换(文件流的应用)

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  5. 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)

    static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...

  6. C#下载文件,Stream 和 byte[] 之间的转换

    stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...

  7. Android Drawable、Bitmap、byte[]之间的转换

    转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...

  8. Image与byte[]之间的转换

    //将image转化为二进制 public static byte[] GetByteImage(Image img) { byte[] bt = null; if (!img.Equals(null ...

  9. C#--整型与字节数组byte[]之间的转换

    using System; int  i = 123;byte [] intBuff = BitConverter.GetBytes(i);     // 将 int 转换成字节数组lob.Write ...

随机推荐

  1. php 获取图片、swf的尺寸大小

    PHP获取图片大小函数.  getimagesize() 能够得到图片及flash(swf)的大小. 语法 1 list($width, $height, $type, $attr) = getima ...

  2. win8.1中如何获得管理员权限步骤

    按WIN+R,运行对话框中输入gpedit.msc,开启组策略, 然后一步步地在"计算机配置"-"Windows 设置"-"安全设置"-&q ...

  3. HTTP访问控制(CORS)

    跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP请求.比如说,域名A(http://domaina.exampl ...

  4. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

  5. Android开发UI之补间动画-布局添加动画

    布局添加动画 使用步骤: 1.获取到布局的id RelativeLayout ly=(RelativeLayout)findViewById(R.id.layout); 2.设置动画样式 ScaleA ...

  6. Java 数组在内存中的结构

    Java中的数组存储两类事物: 原始值(int,char,...),或者引用(对象指针). 当一个对象通过 new 创建,那么将在堆内存中分配一段空间,并且返回其引用(指针). 对于数组,也是同样的方 ...

  7. 使用Visual Studio进行单元测试

    一.使用Visual Studio进行单元测试的几个建议 1.先写单元测试(依我愚见,应该是接口先行,如果有的话) -> 测试失败 -> 以最小的改动(即编写实际代码)使测试通过(而在VS ...

  8. POJ_3273_Monthly_Expense_(二分,最小化最大值)

    描述 http://poj.org/problem?id=3273 共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值. Monthly Expense ...

  9. hdu 4294 数学分析+搜索

    又要开始一段搜索的路程了. 最近看了这题,在网上看到一个结论,任何一个数倍数都能被不超过两个数字组成,假如一个数n个A%x=b,那么必然有m个A%=b那么此时n个A减去m个B就能够被x整除,那么此时就 ...

  10. ruby编程语言-学习笔记3(第4章 表达式和操作符)

    4.6 操作符 了解优先级很重要 位移操作符 (0b1011)<< 1       #   ==> "10110"      11 << 1 = 22 ...