标签:
public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource { public void setBackgroundDrawable(Drawable background) { ..... background.setCallback(this); ...... } } public abstract class Drawable { public final void setCallback(Callback cb) { mCallback = new WeakReference<Callback>(cb); } }
所以最好在 onDestroy() 中调用view.getBackground().setCallback(null);
在stackoverflow的相关问答中出现了通用的unbindDrawables方法
http://stackoverflow.com/questions/9461364/exception-in-unbinddrawables
@Override protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.top_layout)); System.gc(); Drawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); view.setBackground(null); //添加 } if (view instanceof ViewGroup && !(view instanceof AdapterView)) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } }
BitmapFactory.Options options = new BitmapFactory.Options(); //只加载图片的部分信息,减少内存占用 options.inJustDecodeBounds = true; Bitmap tmpBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(new URL(url).openStream()), null, options); //获取图片的长宽像素 int height = options.outHeight; int width = options.outWidth;
(2) 这样就在下载网络图片之前就可以知道图片的大小了,然后根据最终需要显示的图片大小进行压缩(通常Android会在绘制的时候缩放图片)
options = new BitmapFactory.Options(); options.inSampleSize = sampleSize;//压缩比列,如果是3,就会压缩到1/3 bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(new URL(url).openStream()), null, options);
这样获取的图片内存占用会小一点。
标签:
原文地址:http://www.cnblogs.com/dongweiq/p/4241153.html