Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.