Wednesday, October 22, 2014

Center an ImageView inside a parent View in Titanium

As I was beginning my journey with Titanium, one of the hurdles I faced was centering an image. I wanted something that is similar to background-size: cover; in CSS where the image will be maxed out in the parent container while still retaining the image's aspect ratio. Since I can't find a built-in solution, I created one myself.

The centerImageView function stretches the image's smaller side to fit the parent container, and recalculates the height and width so that it will retain it's aspect ratio.

As the saying goes, if there is a wall, we'll break it. If there isn't a path, we'll make one with our hands! I'll be sharing some more snippets here. They will mostly be basic, but I hope they can still be useful to you.

Until next time! :D

No comments:

Post a Comment