Android中 Bitmap和Drawable相互转换的方法
16lz
2022-04-16
1.Drawable—>Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);
Resources res=getResources();
private byte[] Bitmap2Bytes(Bitmap bm){
2.Bitmap—->Drawable
Drawable drawable =new BitmapDrawable(bmp);
3、Drawable → 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;
}
4、从资源中获取Bitmap
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
5、Bitmap → byte[]
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; } }
转载自http://blog.csdn.net/hezhipin610039/article/details/7899248/
更多相关文章
- android 基于百度地图api获取经纬度
- 获取Android的APK包签名信息
- Android(安卓)Studio 获取数字签名信息(SHA1)
- 技术|Android安装包优化
- [整理]android中几种常见的尺寸
- android有用代码片段
- Android(安卓)获取前台应用
- Android之获取手机相关信息
- Android应用程序中创建一个http服务器