Skip to content

Instantly share code, notes, and snippets.

@nickaknudson
Created February 21, 2014 04:17
Show Gist options
  • Save nickaknudson/9128648 to your computer and use it in GitHub Desktop.
Save nickaknudson/9128648 to your computer and use it in GitHub Desktop.
PositionAnimation
Animation translateAnimation = new PositionAnimation(toX, toY, new PositionAnimation.PositionAnimationCallback() {
@Override
public float fromY() {
return view.getY();
}
@Override
public float fromX() {
return view.getX();
}
});
/**
*
*/
package com.nickaknudson.android.animations;
import com.nickaknudson.mva.callbacks.Callback;
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
* @author nick
*
*/
public class PositionAnimation extends Animation {
private Float toX;
private Float toY;
private Float fromX;
private Float fromY;
private PositionAnimationCallback callback;
/**
* @param toX
* @param toY
* @param callback
*/
public PositionAnimation(float toX, float toY, PositionAnimationCallback callback) {
this.toX = toX;
this.toY = toY;
this.callback = callback;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(fromX == null) fromX = callback.fromX();
if(fromY == null) fromY = callback.fromY();
float dx = 0;
float dy = 0;
if (fromX != toX) {
//dx = fromX + ((toX - fromX) * interpolatedTime);
dx = ((toX - fromX) * interpolatedTime);
}
if (fromY != toY) {
//dy = fromY + ((toY - fromY) * interpolatedTime);
dy = ((toY - fromY) * interpolatedTime);
}
t.getMatrix().setTranslate(dx, dy);
}
/**
* @author nick
*/
public interface PositionAnimationCallback extends Callback {
/**
* @return
*/
public float fromX();
/**
* @return
*/
public float fromY();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment