Android: Question about Bitmaps, memory usage, and scaling

前端 未结 3 1818
野性不改
野性不改 2021-02-02 17:01

For readibility, I posted the code examples that my solutions refer to first, and thenI listed the explanations of my solutions in a numerical list.

I have been

相关标签:
3条回答
  • 2021-02-02 17:15

    The problem of the android heap is, that you actually don't know how much of the heap you may use, because any background service could at any time ruin everything for you, if you overstep the memory constraints.

    Why don't you just keep one Bitmap the size of the canvas you are always drawing on, and a stack of downsampled bitmaps? You could then render all image in native solution to your canvas, and always draw your downsampled bitmaps for any change that occurs. Once the change is over, or it is clear which image is most important, redraw that one in native resolution to the canvas (by accessing the disk again).

    0 讨论(0)
  • 2021-02-02 17:20

    The right approach is to decode for the size you need to show and subsample the image in tiles when you need to zoom.

    There's a library that should do exactly this, check it out: https://github.com/davemorrissey/subsampling-scale-image-view

    0 讨论(0)
  • 2021-02-02 17:28

    A simple and straightforward way is to use "inJustDecodeBounds" property of Options. Set this property to true for options object you created, then continue to decode stream.

    The bitmap returned will be null, which means no memory is allocated to it, but you can read the dimensions of the bitmap and thus determine its size and adjust the inSampleSize ratio.

    Later reset inJustDecodeBounds to false, by now you know the scale down factor, thus bitmap of required size can now be generated.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题