Skip to content

Instantly share code, notes, and snippets.

@Arood
Created May 25, 2013 13:18
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 Arood/5649012 to your computer and use it in GitHub Desktop.
Save Arood/5649012 to your computer and use it in GitHub Desktop.
A wrapper for http://billdawson.github.io/ti-android-animation/, to make the API more like the regular Ti.UI.Animation
/**
* A wrapper for http://billdawson.github.io/ti-android-animation/,
* to make the API more like the regular Ti.UI.Animation
*
* Notes:
* - I haven't added a wrapper for "curving" yet
* - Added "scale", "translateX" and "translateY" as separate properties that can be used instead of "transform" on iOS
* - You can only set "top" and "left" on Android, "right" or "bottom" doesn't work (since the position is animated in X or Y)
* - AnchorPoint doesn't seem to work on Android
*
* Usage:
* animate(view, options, onComplete);
*
* Example:
* var animate = require("/animate"),
* view = Ti.UI.createView({ top: 50, left: 50, width: 100, height: 100, backgroundColor: "red" });
* animate(view, {
* top: 100,
* left: 100
* }, function() {
* Ti.API.info("Animation complete");
* });
*/
if (Ti.Platform.name == "android") {
var mod = require("com.billdawson.timodules.animation");
}
module.exports = function(view, options, onComplete) {
onComplete = onComplete || function() {};
if (Ti.Platform.name == "android") {
var animation = mod.viewPropertyAnimator.animate(view).withEndAction(onComplete);
if (options.duration != undefined) {
animation.setDuration(options.duration);
}
if (options.left != undefined) {
animation.x(options.left);
}
if (options.top != undefined) {
animation.y(options.top);
}
if (options.opacity != undefined) {
animation.alpha(options.opacity);
}
if (options.scale != undefined) {
animation.scaleX(options.scale);
animation.scaleY(options.scale);
}
if (options.translateX != undefined || options.translateY != undefined) {
animation.translationX(options.translateX || 0);
animation.translationY(options.translateY || 0);
}
} else {
if (options.scale != undefined || options.translateX != undefined || options.translateY != undefined) {
var matrixOptions = {};
if (options.scale != undefined) {
matrixOptions.scale = options.scale;
delete options.scale;
}
var matrix = Ti.UI.create2DMatrix(matrixOptions);
if (options.translateX != undefined || options.translateY != undefined) {
matrix.translate(options.translateX || 0, options.translateY || 0);
}
options.transform = matrix;
}
view.animate(options, onComplete);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment