Android 中 Bitmap,byte[],Drawable
相互转化
一、相关概念
1、Drawable 就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能
是一个图形(ShapeDrawable),还有可能是一个图层( LayerDrawable),我们根
据画图的需求,创建相应的可画对象
2、Canvas 画布,绘图的目的区域,用于绘图
3、Bitmap 位图,用于图的处理
4、Matrix 矩阵
二、Bitmap
1、从资源中获取 Bitmap
1 Resources res = getResources();
2 Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
2、Bitmap → byte[]
1 public byte[] Bitmap2Bytes(Bitmap bm) {
2 ByteArrayOutputStream baos = new ByteArrayOutputStream();
3 bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
4 return baos.toByteArray();
5 }
3、byte[] → Bitmap
1 public Bitmap Bytes2Bimap(byte[] b) {
2 if (b.length != 0) {
3 return BitmapFactory.decodeByteArray(b, 0, b.length);
4 } else {
5 return null;
6 }