Created
February 25, 2013 08:00
-
-
Save cathandnya/5028405 to your computer and use it in GitHub Desktop.
ScalableImageView #titanium
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
var ScalableImageView = function(params) { | |
var self = Ti.UI.createImageView(params); | |
if (params.scaleType != 'aspectFit') { | |
self.addEventListener('load', function(e) { | |
var image = self.toBlob(); | |
var selfBlob = self.toImage(); | |
if (params.scaleType == 'aspectFill') { | |
var imageWidth = image.width; | |
var imageHeight = image.height; | |
var crop = maxCenter(selfBlob, { | |
x : 0, | |
y : 0, | |
width : imageWidth, | |
height : imageHeight, | |
}); | |
self.image = image.imageAsCropped(crop); | |
} else if (params.scaleType == 'fill') { | |
var w, h; | |
w = selfBlob.width; | |
h = selfBlob.height; | |
self.image = image.imageAsResized(w, h); | |
} | |
}); | |
} | |
var maxCenter = function(s, ir) { | |
var result = {}; | |
if (ir.height / ir.width > s.height / s.width) { | |
result.width = ir.width; | |
result.height = s.height * ir.width / s.width; | |
result.x = ir.x; | |
result.y = ir.y + (ir.height - result.height) / 2; | |
} else { | |
result.height = ir.height; | |
result.width = s.width * ir.height / s.height; | |
result.y = ir.y; | |
result.x = ir.x + (ir.width - result.width) / 2; | |
} | |
return result; | |
} | |
return self; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment