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

来源:互联网 发布:厦门美工培训班 编辑:程序博客网 时间:2024/05/22 04:39

一、相关概念

1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵

很多Android开发者可能发现,将Bitmap转为字节数组可能文件大小和原始图片差异很大,代码如下

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

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

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

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

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

二、Bitmap

1、从资源中获取Bitmap

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


2、Bitmap → byte[]

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


3、byte[] → Bitmap

 

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

 

4、Bitmap缩放

1publicstaticBitmap zoomBitmap(Bitmap bitmap,intwidth,intheight) {2intw = bitmap.getWidth();3inth = bitmap.getHeight();4Matrix matrix =newMatrix();5floatscaleWidth = ((float) width / w);6floatscaleHeight = ((float) height / h);7matrix.postScale(scaleWidth, scaleHeight);8Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,true);9returnnewbmp;10}

 

5、将Drawable转化为Bitmap

1publicstaticBitmap drawableToBitmap(Drawable drawable) {2//取 drawable 的长宽3intw = drawable.getIntrinsicWidth();4inth = drawable.getIntrinsicHeight();56//取 drawable 的颜色格式7Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_88888: Bitmap.Config.RGB_565;9//建立对应 bitmap10Bitmap bitmap = Bitmap.createBitmap(w, h, config);11//建立对应 bitmap 的画布12Canvas canvas =newCanvas(bitmap);13drawable.setBounds(0, 0, w, h);14//把 drawable 内容画到画布中15drawable.draw(canvas);16returnbitmap;17}

 

6、获得圆角图片 

1publicstaticBitmap getRoundedCornerBitmap(Bitmap bitmap,floatroundPx) {2int3inth = bitmap.getHeight();4Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);5Canvas canvas =newCanvas(output);6finalintcolor = 0xff424242;7finalPaint paint =newPaint();8finalRect rect =newRect(0, 0, w, h);9finalRectF rectF =newRectF(rect);10paint.setAntiAlias(true);11canvas.drawARGB(0, 0, 0, 0);12paint.setColor(color);13canvas.drawRoundRect(rectF, roundPx, roundPx, paint);14paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));15canvas.drawBitmap(bitmap, rect, rect, paint);1617returnoutput;18}

 

7、获得带倒影的图片


1publicstaticBitmap createReflectionImageWithOrigin(Bitmap bitmap) {2finalintreflectionGap = 4;3intw = bitmap.getWidth();4inth = bitmap.getHeight();56Matrix matrix =newMatrix();7matrix.preScale(1, -1);89Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,10h / 2, matrix,false);1112Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),13Config.ARGB_8888);1415Canvas canvas =newCanvas(bitmapWithReflection);16canvas.drawBitmap(bitmap, 0, 0,null);17Paint deafalutPaint =newPaint();18canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);1920canvas.drawBitmap(reflectionImage, 0, h + reflectionGap,null);2122Paint paint =newPaint();23LinearGradient shader =newLinearGradient(0, bitmap.getHeight(), 0,24bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,250x00ffffff, TileMode.CLAMP);26paint.setShader(shader);27//Set the Transfer mode to be porter duff and destination in28paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));29//Draw a rectangle using the paint with our linear gradient30canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()31+ reflectionGap, paint);3233returnbitmapWithReflection;34}

 

 三、Drawable

1、Bitmap转换成Drawable

1Bitmap bm=xxx;//xxx根据你的情况获取2BitmapDrawable bd=newBitmapDrawable(getResource(), bm);3因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

2、Drawable缩放


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

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

一般而言,回收bitmap内存可以用到以下代码

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

在这里要声明一下,bitmap可以有多个(以为着可以有多个if语句),但System.gc()最好只有一个(所以我将它写在了if语句外),因为System.gc()

每次调用都要将整个内存扫描一遍,因而如果多次调用的话会影响程序运行的速度。为了程序的效率,我将它放在了所有回收语句之后,

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

回收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内存溢出问题,但是具体情况还是要具体分析。

原创粉丝点击