Bitmap释放

来源:互联网 发布:linux程序设计第4版pdf 编辑:程序博客网 时间:2024/05/01 21:01

   Bitmap没有及时释放的话经常会出现OOM错误。由于Bitmap占用的是底层C的内存,JVM的垃圾回收机制对他没有用。用完后必须显示的调用 recycle();告诉虚拟器该Bitmap没有用了,可以释放了,能后虚拟器才能在稍后的时候释放。

 

1.Bitmap变量

[java] view plaincopy
  1. Bitmap bit = BitmapFactory.decodeFile(path);  
  2. if(bit != null && !bit.isRecycled()) {  
  3.     bit.recycle();  
  4. }  

 

2.Drawable

先把Drawable转成 BitmapDrawable 在释放 

[java] view plaincopy
  1. BitmapDrawable bd = (BitmapDrawable)d;  
  2. if(!bd.getBitmap().isRecycled()) {   
  3.     bd.getBitmap().recycle();  
  4. }  


3.public void drawBitmap(int[] colors, int offset, int stride, float x,
                           float y, int width, int height, boolean hasAlpha,
                           Paint paint)



//c语言的方法。 // 它采用两个浮点数作为参数,分别表示在每个轴上所产生的缩放量。第一个参数是x轴的缩放比例,而第二个参数是y轴的缩放比例。
tempcavas.drawBitmap(temp_bitmap, matrix, mPaint);// 绘制图像//在前面//
// 刚开始给一张透明画布上画的图片。
temp_bitmap.recycle();//回收

0 0