Skip to content

Instantly share code, notes, and snippets.

@vyatri
Created March 20, 2014 10:14
Show Gist options
  • Save vyatri/9660818 to your computer and use it in GitHub Desktop.
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
$.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%";
}
});
<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