Drawable、Bitmap、byte[]之间的转换
2014-01-17 13:32
309 查看
1、Drawable → Bitmap
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
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
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
4、 byte[] → Bitmap
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
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; }
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
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
4、 byte[] → Bitmap
Java代码
![](http://bingtian.iteye.com/images/icon_copy.gif)
![](http://bingtian.iteye.com/images/icon_star.png)
![](http://bingtian.iteye.com/images/spinner.gif)
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
相关文章推荐
- Drawable、Bitmap、byte[]之间的转换(转)
- Android Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换问题
- android Bitmap与Drawable与byte[]与InputStream之间的转换工具类
- Drawable、Bitmap、byte[]之间的转换
- Android中Bitmap与Drawable、byte[ ]之间的转换
- 【Android】Bitmap、InputStream、Drawable、byte[]、Base64之间的相互转换
- Drawable、Bitmap、byte[]之间的转换(转)
- 【android】bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- 安卓图片--Bitmap与Drawable与byte[]与InputStream之间的转换工具类【转】
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Drawable、Bitmap、byte[]之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- android byte数组,bitmap,drawable之间的转换
- Drawable、Bitmap、byte[]之间的转换
- byte[],bitmap,drawable之间的相互转换