Android中Bitmap,byte[],Drawable相互转化

一、Drawable就是一个可画的对象,其多是一张位图(BitmapDrawable),也多是一个图形(ShapeDrawable),还有多是一个图层(LayerDrawable),咱们根据画图的需求,建立相应的可画对象
二、Canvas画布,绘图的目的区域,用于绘图
三、Bitmap位图,用于图的处理
四、Matrix矩阵

不少Android开发者可能发现,将Bitmap转为字节数组可能文件大小和原始图片差别很大,代码以下php

1. 字节数组data保存Bitmap对象转为字节数组,处理代码:android

BitmapFactory.decodeByteArray(data, 0, data.length);canvas

2. 而第二种方法处理代码:数组

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data2 = baos.toByteArray();post

这里其实很好理解,第二种方法使用了Bitmap的compress方法,通常用于保存一个Bitmap对象,转为字节输出流,可是compress目前编码由两种,好比JPG,通常处理照片和PNG,PNG通常处理带Alpha透明通道的图片,后面的第二个参数是清晰度,通常100是最高,0是最低,这个值越大图片越清晰,同时文件体积越大,JPG和PNG都是压缩的图片,因此和原始的直接经过BitmapFactory.decodeByteArray解码后的大小会有很大的不一样。测试

2、Bitmap

一、从资源中获取Bitmap

1 Resources res = getResources(); 2 Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);


二、Bitmap → byte[]

1publicbyte[] Bitmap2Bytes(Bitmap bm) { 2 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 3 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 4return baos.toByteArray(); 5 }


三、byte[] → Bitmap

1public Bitmap Bytes2Bimap(byte[] b) { 2if (b.length != 0) { 3return BitmapFactory.decodeByteArray(b, 0, b.length); 4 } else { 5returnnull; 6 } 7 }

四、Bitmap缩放

 1publicstatic Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { 2int w = bitmap.getWidth(); 3int h = bitmap.getHeight(); 4 Matrix matrix = new Matrix(); 5float scaleWidth = ((float) width / w); 6float scaleHeight = ((float) height / h); 7 matrix.postScale(scaleWidth, scaleHeight); 8 Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); 9return newbmp; 10 }

五、将Drawable转化为Bitmap

 1publicstatic Bitmap drawableToBitmap(Drawable drawable) { 2// 取 drawable 的长宽 3int w = drawable.getIntrinsicWidth(); 4int h = drawable.getIntrinsicHeight(); 5 6// 取 drawable 的颜色格式 7 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 8 : Bitmap.Config.RGB_565; 9// 创建对应 bitmap 10 Bitmap bitmap = Bitmap.createBitmap(w, h, config); 11// 创建对应 bitmap 的画布 12 Canvas canvas = new Canvas(bitmap); 13 drawable.setBounds(0, 0, w, h); 14// 把 drawable 内容画到画布中 15 drawable.draw(canvas); 16return bitmap; 17 }

六、得到圆角图片

 1publicstatic Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { 2int w = bitmap.getWidth(); 3int h = bitmap.getHeight(); 4 Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888); 5 Canvas canvas = new Canvas(output); 6finalint color = 0xff424242; 7final Paint paint = new Paint(); 8final Rect rect = new Rect(0, 0, w, h); 9final RectF rectF = new RectF(rect); 10 paint.setAntiAlias(true); 11 canvas.drawARGB(0, 0, 0, 0); 12 paint.setColor(color); 13 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 14 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 15 canvas.drawBitmap(bitmap, rect, rect, paint); 16 17return output; 18 }

七、得到带倒影的图片


 1publicstatic Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { 2finalint reflectionGap = 4; 3int w = bitmap.getWidth(); 4int h = bitmap.getHeight(); 5 6 Matrix matrix = new Matrix(); 7 matrix.preScale(1, -1); 8 9 Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, 10 h / 2, matrix, false); 11 12 Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), 13 Config.ARGB_8888); 14 15 Canvas canvas = new Canvas(bitmapWithReflection); 16 canvas.drawBitmap(bitmap, 0, 0, null); 17 Paint deafalutPaint = new Paint(); 18 canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint); 19 20 canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null); 21 22 Paint paint = new Paint(); 23 LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, 24 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 25 0x00ffffff, TileMode.CLAMP); 26 paint.setShader(shader); 27// Set the Transfer mode to be porter duff and destination in 28 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 29// Draw a rectangle using the paint with our linear gradient 30 canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() 31 + reflectionGap, paint); 32 33return bitmapWithReflection; 34 }

3、Drawable

一、Bitmap转换成Drawable

1 Bitmap bm=xxx; //xxx根据你的状况获取 2 BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 3 由于BtimapDrawable是Drawable的子类,最终直接使用bd对象便可。

二、Drawable缩放


 1publicstatic Drawable zoomDrawable(Drawable drawable, int w, int h) { 2int width = drawable.getIntrinsicWidth(); 3int height = drawable.getIntrinsicHeight(); 4// drawable转换成bitmap 5 Bitmap oldbmp = drawableToBitmap(drawable); 6// 建立操做图片用的Matrix对象 7 Matrix matrix = new Matrix(); 8// 计算缩放比例 9float sx = ((float) w / width); 10float sy = ((float) h / height); 11// 设置缩放比例 12 matrix.postScale(sx, sy); 13// 创建新的bitmap,其内容是对原bitmap的缩放后的图 14 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, 15 matrix, true); 16returnnew BitmapDrawable(newbmp); 17 }
android BitMap回收
2012-06-26 11:12

第一种方法--及时回收bitmap内存:编码

通常而言,回收bitmap内存能够用到如下代码spa

if(bitmap != null && !bitmap.isRecycled()){
bitmap.recycle();
bitmap = null;
}
System.gc();
bitmap.recycle()方法用于回收该bitmap所占用的内存,接着将bitmap置空,最后,别忘了用System.gc()调用一下系统的垃圾回收器。code

在这里要声明一下,bitmap能够有多个(觉得着能够有多个if语句),但System.gc()最好只有一个(因此我将它写在了if语句外),由于System.gc()orm

每次调用都要将整个内存扫描一遍,于是若是屡次调用的话会影响程序运行的速度。为了程序的效率,我将它放在了全部回收语句以后,

这样已经起到了它的效果,还节约的时间。

回收bitmap已经知道了,那么“及时”怎么理解呢?

根据个人实际经验,bitmap发挥做用的地方要么在View里,要么在Activity里(固然确定有其余区域,可是原理都是相似的),

回收bitmap的地方最好写在这些区域刚刚不使用bitmap了的时刻。

好比说View若是使用了bitmap,就应该在这个View再也不绘制了的时候回收,或者是在跳转到的下一个区域的代码中回收;

再好比说SurfaceView,就应该在onSurfaceDestroyed这个方法中回收;

同理,若是Activity使用了bitmap,就能够在onStop或者onDestroy方法中回收......

结合以上的共同点,“及时回收”的原理就是在使用了bitmap的区域结束时或结束后回收。


第二种方法--压缩图片:

这个方法固然很简单了,就是使图片体积大小变小,

能够有两种方式:

一种是使图片质量下降(分辨率不变),

另外一种是使图片分辨率下降(分辨率改变)。

总之,使图片大小变小就好了。

实践证实,使图片质量下降(分辨率不变)能够大幅度地减少体积,并且质量的差别肉眼看上去并不明显。

我刚开始使用的就是这两种方法,原理很简单,但是,个人BUG发生虽然没那么频繁了,可是它依然存在!!

后来通过几天的努力与尝试,结合我项目的一些具体状况,我终于解决了这个使人头痛的BUG,可是事实却有点出乎个人意料。

当我使用了上述两种方法BUG依然还没解决的时候,我开始怀疑,bitmap超过8M会报错,可如今我把前先后后的bitmap都回收了,

不可能还有8M了,那为何还会报错呢?

终于我发现了这个缘由:当内存中已经被一些bitmap使用过以后,不管被回收与否,它都会变得特别“敏感”,这个时候,

若是bitmap忽然要占用大量的内存,即便和以前已经剩下的内存加起来不到8M,系统也会报错,缘由是它变“敏感”了!

我不知道这个用底层原理如何解释比较好,可是我想“敏感”这个词应该能够很形象地进行解释。

因而,为了顺应内存的“敏感性”,我将那个须要同时装载多个大致积bitmap的地方进行了修改,用到了如下方法:

//压缩,用于节省BITMAP内存空间--解决BUG的关键步骤
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2; //这个的值压缩的倍数(2的整数倍),数值越小,压缩率越小,图片越清晰

//返回原图解码以后的bitmap对象
bitmap = BitmapFactory.decodeResource(Context, ResourcesId, opts);
即先将图片缩小一倍,再将这缩小了一倍的图片做为bitmap存入内存,这样一来,它占用的bitmap内存大大减少。

后来经测试,BUG果真解决了。图片缩小一倍后,顺应了内存的“敏感性”,也就不会再报错了。

以上方法应该足以解决大多数bitmap内存溢出问题,可是具体状况仍是要具体分析。

相关文章
相关标签/搜索