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

来源:互联网 发布:淘宝开店注册在哪里 编辑:程序博客网 时间:2024/06/01 18:40

一、相关概念

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

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


2、Bitmap → byte[]

public byte[] Bitmap2Bytes(Bitmap bm) {   ByteArrayOutputStream baos = new ByteArrayOutputStream();   bm.compress(Bitmap.CompressFormat.PNG, 100, baos);   return baos.toByteArray();}


3、byte[] → Bitmap

 

public Bitmap Bytes2Bimap(byte[] b) {  if (b.length != 0) {    return BitmapFactory.decodeByteArray(b, 0, b.length);  } else {    returnnull;  }}

 

4、Bitmap缩放

 public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {   int w = bitmap.getWidth();   int h = bitmap.getHeight();   Matrix matrix = new Matrix();   float scaleWidth = ((float) width / w);   float scaleHeight = ((float) height / h);   matrix.postScale(scaleWidth, scaleHeight);   Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);   return newbmp;}

 

5、将Drawable转化为Bitmap

 public static Bitmap drawableToBitmap(Drawable drawable) {// 取 drawable 的长宽   int w = drawable.getIntrinsicWidth();   int h = drawable.getIntrinsicHeight();    // 取 drawable 的颜色格式   Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888    : Bitmap.Config.RGB_565;   // 建立对应 bitmap   Bitmap bitmap = Bitmap.createBitmap(w, h, config);   // 建立对应 bitmap 的画布   Canvas canvas = new Canvas(bitmap);   drawable.setBounds(0, 0, w, h);   // 把 drawable 内容画到画布中   drawable.draw(canvas);   return bitmap;
}

 

6、获得圆角图片 

 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {   int w = bitmap.getWidth();   int h = bitmap.getHeight();   Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);   Canvas canvas = new Canvas(output);   finalint color = 0xff424242;   final Paint paint = new Paint();   final Rect rect = new Rect(0, 0, w, h);   final RectF rectF = new RectF(rect);   paint.setAntiAlias(true);   canvas.drawARGB(0, 0, 0, 0);   paint.setColor(color);   canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));   canvas.drawBitmap(bitmap, rect, rect, paint);   return output; }

 

7、获得带倒影的图片


 public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {  finalint reflectionGap = 4;  int w = bitmap.getWidth();  int h = bitmap.getHeight();   Matrix matrix = new Matrix();  matrix.preScale(1, -1);   Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,  h / 2, matrix, false);   Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),  Config.ARGB_8888);   Canvas canvas = new Canvas(bitmapWithReflection);  canvas.drawBitmap(bitmap, 0, 0, null);  Paint deafalutPaint = new Paint();  canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);   canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);   Paint paint = new Paint();  LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  0x00ffffff, TileMode.CLAMP);  paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in  paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient  canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()   + reflectionGap, paint);   return bitmapWithReflection;
}

 

 三、Drawable

1、Bitmap转换成Drawable

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

2、Drawable缩放


publicstatic Drawable zoomDrawable(Drawable drawable, int w, int h) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); // drawable转换成bitmap Bitmap oldbmp = drawableToBitmap(drawable); // 创建操作图片用的Matrix对象 Matrix matrix = new Matrix(); // 计算缩放比例float sx = ((float) w / width); float sy = ((float) h / height); // 设置缩放比例 matrix.postScale(sx, sy); // 建立新的bitmap,其内容是对原bitmap的缩放后的图 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,  matrix, true); returnnew BitmapDrawable(newbmp);  }
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 0
原创粉丝点击