Drawable、Bitmap、byte[]之间的转换 (转)
来源:互联网 发布:alias软件购买 编辑:程序博客网 时间:2024/05/06 10:43
Drawable、Bitmap、byte[]之间的转换 (转)
http://www.eoeandroid.com/forum-viewthread-tid-57806-fromuid-39187.html
android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:
1、Drawable → Bitmap 的简单方法
((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();
2、Drawable → Bitmap
Java代码
public static Bitmap drawableToBitmap(Drawable drawable) {
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);
//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);
3、从资源中获取Bitmap
Java代码
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
4、Bitmap → byte[]
Java代码
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
5、 byte[] → Bitmap
Java代码
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
1、Drawable → Bitmap 的简单方法
((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();
2、Drawable → Bitmap
Java代码
public static Bitmap drawableToBitmap(Drawable drawable) {
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);
//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);
3、从资源中获取Bitmap
Java代码
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
4、Bitmap → byte[]
Java代码
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
5、 byte[] → Bitmap
Java代码
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
- Drawable、Bitmap、byte[]之间的转换 (转)
- Drawable、Bitmap、byte[]之间的转换 (转)
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换问题
- Drawable, Bitmap, byte[]之间的转换
- Drawable、Bitmap和byte[]之间的转换
- AddressBook 联系人字母排序法
- OBJ文件格式(二)
- Makefile wildcard notdir patsubst 简介
- STUN在SIP中的工作原理及过程(转载)
- H264参数结构一:元素的分层结构
- Drawable、Bitmap、byte[]之间的转换 (转)
- 远程桌面开启“复制粘贴”功能
- 标识接口 序列化
- ALSA Audio API 使用指南(中英版)
- 在窗口中贴图
- JACL和Eclipse结合编程
- 2011春节后
- ProgressDialog使用
- Qt:在TreeModel+QTreeView中使用复选框(checkbox)