Created
February 21, 2014 04:17
-
-
Save nickaknudson/9128648 to your computer and use it in GitHub Desktop.
PositionAnimation
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
Animation translateAnimation = new PositionAnimation(toX, toY, new PositionAnimation.PositionAnimationCallback() { | |
@Override | |
public float fromY() { | |
return view.getY(); | |
} | |
@Override | |
public float fromX() { | |
return view.getX(); | |
} | |
}); |
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
/** | |
* | |
*/ | |
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