I thought it should be the easiest task in the world, but it turned out to be much more complicated than I thought.
I want to derive my own View class from an ImageView, extend its onDraw method just to overlay a few more elements on the underlying image displayed by the ImageView itself.
开发者_Python百科The problem is: the image will be scaled to preserve its ratio, so sometimes next to the image there will be margins - offsets, I call them. To place my own elements upon the image, I need to know these offsets and the scaling performed on the image - but I am not getting a clue anywhere.
I thought I just need to call ImageView methods like "getImageLeft()", "getImageTop()", etc - but there are no such methods!!
I found one solution here that involved a whole lot of calculation on my own, I find that way to complicated (and it did not work...).
Can anybody give me a clue??
Thanks!
Use paddings. They determine the offset inside a view. There is also an attribute scaleType which determines how the image should be scaled if its size doesn't match with the size of the imageView. If you want to know how the image is scaled inside and imageView I can say that the proportions are kept. For example you have an image 200x50 and ImageView 100x100. Then the size of the image in the imageView must be 100x25. After scaling the image is placed in the center of the ImageView by default. So you have about 12 extra space from top and bottom.
精彩评论