Created
March 20, 2014 10:14
-
-
Save vyatri/9660818 to your computer and use it in GitHub Desktop.
How to center ImageView inside ScrollView in Titanium. A way to "automatically" centering image inside scrollview for automatic center cropping or thumbnailing purpose. If you use this for model-view binding, simply put this controller script inside filter function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.bar.addEventListener("load",function(e){ // because the image in $.bar is remote | |
// check orientasi | |
var h = $.bar.rect.height, w = $.bar.rect.width, a = w/h; | |
var ch = $.foo.rect.height, cw = $.foo.rect.width, ac = cw/ch; // container | |
if(a > ac){ | |
$.foo.contentHeight = "100%"; | |
$.bar.left = (w - cw)/2*-1; | |
} else if(a < ac){ | |
$.foo.contentWidth = "100%"; | |
$.bar.top = ((cw/a)-ch)/2*-1; | |
} else { | |
$.foo.contentWidth = "100%"; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ScrollView id="foo" backgroundColor="blue" width="200" height="200" scrollingEnabled="false"> | |
<ImageView id="bar" image="http://xrozz.com/pullman/uploads/images/Artwork5jpg13032014103246.jpg" /> | |
</ScrollView> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment