1

Firstly I am aware of the recommended approach of using inJustDecodeBounds and inSample size to load bitmaps at a size close to the desired size. This is however a fairly broad approach that only gets an image approximate to the target.

I have though of utilising options.inDensity and options.inTargetDensity to trick the native loader into scaling an image more precisely to the desired target size. Basically I set options.inDensity to the actual width of the image and options.inTargetDensity to the desired width and I do indeed get an image at the desired size (aspect ration happens to remain the same in this case). I then set image.setDensity(DENSITY_NONE) on the resulting image and all appears to work OK.

Anyone know of anything wrong with this approach? Any thoughts on memory efficiency and image quality?

user1016143
  • 71
  • 1
  • 6
  • Sounds brilliant to me! (Can't believe android devs wrote the code but didn't expose the functionality in a sane and sensible way). – Robin Davies Feb 16 '13 at 19:42
  • sounds cool! could you please post a sample ? – android developer Feb 16 '13 at 19:52
  • i think though that using inSample is more performance-optimized , no ? because it's easier for the CPU/GPU to handle sampling this way , no? – android developer Feb 16 '13 at 20:01
  • I would imagine that one could combine InSampleSize with this approach. Not tested but if you can calculate the resulting size of the image AFTER the InSampleSize effect and use this in options.inDensity it may well work. – user1016143 Feb 17 '13 at 12:01
  • I have now tested with inSampleSize as I suggest above and it does indeed seem to work. – user1016143 Feb 17 '13 at 13:22

3 Answers3

0

I have always got better image management with Opengl 2.0 and surface views.

rock_win
  • 755
  • 1
  • 5
  • 14
0

Sounds brilliant to me! (Can't believe android devs wrote the code but didn't expose the functionality in a sane and sensible way).

I do have one concern. I have good reason to believe that Android is unable to deal with instantiated bitmaps that are larger than 2048x2048 pixels in either dimension. If the internal code to do the rescaling isn't sufficiently intelligent, it may fail when loading bitmaps larger than 2048x2048.

Robin Davies
  • 7,547
  • 1
  • 35
  • 50
  • I would imagine that one could combine InSampleSize with this approach. Not tested but if you can calculate the resulting size of the image AFTER the InSampleSize effect and use this in options.inDensity it may well work. – user1016143 Feb 17 '13 at 11:20
0

I was thinking about this my self, using inDensity and inTargetDensity to scale up/down bitmap on decode. It works well, but unfortunately it yields very bad performance (animation) results. I was hoping I could use this as a "universal" aproach to scale up/down on decode, similar to inSampleSize which is unfortunately only for down sampling. Seems like there is different native implementation: inSampleSize performs well, no obvious performance impact, where inDensity/inTargetDensity introduced noticable performance impact (like slow motion).

Or am I missing something?

hpet
  • 299
  • 2
  • 14
  • I suggest it would be advisable to have pre-scaled images in memory for efficient animations if memory permits. – user1016143 Feb 18 '13 at 17:07