Recycle ImageView's Bitmap

后端 未结 4 748
谎友^
谎友^ 2020-11-27 14:06

I have something like this:

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
WeakReference bm = new WeakReference(Bitmap.createBitma         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 14:44

    If you set the same bitmap object on all your ImageViews, it shouldn't throw an OutOfMemoryError. Basically, this should work:

    WeakReference bm = new WeakReference(Bitmap.createBitmap(3000 + 3000, 2000, Bitmap.Config.ARGB_8888));
    
    Canvas canvas = new Canvas(bm.get());
    canvas.drawBitmap(firstBitmap, 0, 0, null);
    canvas.drawBitmap(bm, firstBitmap.getWidth(), 0, null);
    
    imageView1.setImageBitmap(bm.get());
    imageView2.setImageBitmap(bm.get());
    imageView3.setImageBitmap(bm.get());
    imageView4.setImageBitmap(bm.get());
    imageView5.setImageBitmap(bm.get());
    // ...
    

    If this doesn't work, it simply means your bitmap is too large (6000x2000 pixels is about 12 megabytes, if I calculated right). You can either:

    • make your bitmap smaller
    • cut down on other stuff that use a lot of memory

提交回复
热议问题