Skip to content

Instantly share code, notes, and snippets.

@cathandnya
Created February 25, 2013 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathandnya/5028405 to your computer and use it in GitHub Desktop.
Save cathandnya/5028405 to your computer and use it in GitHub Desktop.
ScalableImageView #titanium
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