Drawable、Bitmap、byte[]之间的转换
2010-09-06 10:08
330 查看
1、Drawable → Bitmap
2、Bitmap → byte[]
3、byte[] → Bitmap
另外还有更简单的方法:
1、Bitmap to Drawable
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;
Drawable drawable = (Drawable)bitmapDrawable;
Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap);
2、Drawable to Bitmap
Drawable d = ImagesList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
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; }
2、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
3、byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){ if(b.length!=0){ return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } }
另外还有更简单的方法:
1、Bitmap to Drawable
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;
Drawable drawable = (Drawable)bitmapDrawable;
Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap);
2、Drawable to Bitmap
Drawable d = ImagesList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
相关文章推荐
- Drawable、Bitmap、byte[]之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Drawable Bitmap byte[] outputStream BitmapFactory之间的转换
- android byte数组,bitmap,drawable之间的转换
- byte[],bitmap,drawable之间的相互转换
- Drawable、Bitmap和byte[]之间的转换
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Android中Bitmap, ID,Drawable, Byte之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Android Drawable、Bitmap、byte[]之间的转换
- Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Android Drawable、Bitmap、byte[]之间的转换
- 转载 Drawable、Bitmap、byte[]之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Drawable、Bitmap、byte[]之间的转换
- android开发之Bitmap 、byte[] 、 Drawable之间的相互转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类