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

一、相关概念

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
1 Resources res = getResources();
2 Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);


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


3、byte[] → Bitmap

 

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

 

4、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 }

 

5、将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 }

 

6、获得圆角图片 
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 }

 

7、获得带倒影的图片


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 }

 

 三、Drawable

1、Bitmap转换成Drawable
1 Bitmap bm=xxx; //xxx根据你的情况获取
2 BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 
3 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
2、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内存可以用到以下代码

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值