I'm implementing a custom ImageView
that have to load a Bitmap
from disk. The Bitmap
should be scaled exactly so, that its width must be equal to the final width a parent layout has been allocated to my ImageView
and height is calculated to keep an aspect ratio.
So, the question is what is the proper place during the View
life cycle to put heavy operations that depend on View
's dimensions known after the layout is done?
(I'd like to keep it simple and not use threads, however.)
- Most likely it's a bad idea to put that code in
onDraw()
method, since it should be as efficient as possible. From the other hand, myImageView
is unlikely to be resized, so it would kill only the firstonDraw()
's performance and it wouldn't affect any subsequent calls toonDraw()
. So I'm not sure about this option. - I could put it at the end of
onLayout(boolean changed, int l, int t, int r, int b)
executing my heavy codeif (changed == true)
. Is this a good idea? - The 3rd option I see is to do it in the
onSizeChanged()
callback. My only concern here is whether this callback is guaranteed to be called only once per actual view's size change. I'm inclined to use this option, since in my tests it works fine.
Could someone give an insight on this?