android 释放图片资源

1.1 主动释放Bitmap资源

+当你确定这个Bitmap资源不会再被使用的时候(当然这个Bitmap不释放可能会让程序下一次启动或者resume快一些,但是其占用的内存资源太大,可能导致程序在后台的时候被杀掉,反而得不偿失),我们建议手动调用recycle()方法,释放其Native内存: if(bitmap != null && !bitmap.isRecycled()){
bitmap.recycle(); bitmap = null; }

1.2 主动释放ImageView的图片资源

+由于我们在实际开发中,很多情况是在xml布局文件中设置ImageView的src或者在代码中调用ImageView.setImageResource/setImageURI/setImageDrawable等方法设置图像,下面代码可以回收这个ImageView所对应的资源:

private static void recycleImageViewBitMap(ImageView imageView) { if (imageView != null) { BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable(); rceycleBitmapDrawable(bd); } }

private static void rceycleBitmapDrawable(BitmapDrawable bitmapDrawable) { if (bitmapDrawable != null) { Bitmap bitmap = bitmapDrawable.getBitmap(); rceycleBitmap(bitmap); } bitmapDrawable = null; }

private static void rceycleBitmap(Bitmap bitmap) { if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; } }

1.3 主动释放ImageView的背景资源

+如果你的ImageView是有Background,那么下面的代码可以释放他: public static void recycleBackgroundBitMap(ImageView view) { if (view != null) { BitmapDrawable bd = (BitmapDrawable) view.getBackground(); rceycleBitmapDrawable(bd); } }

public static void recycleImageViewBitMap(ImageView imageView) { if (imageView != null) { BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable(); rceycleBitmapDrawable(bd); } }

private static void rceycleBitmapDrawable(BitmapDrawable bitmapDrawable) { if (bitmapDrawable != null) { Bitmap bitmap = bitmapDrawable.getBitmap(); rceycleBitmap(bitmap); } bitmapDrawable = null; }

转载于:https://my.oschina.net/u/556624/blog/484967

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值