Skip to content

Instantly share code, notes, and snippets.

@TakamiChie
Created September 20, 2013 16:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save TakamiChie/6640202 to your computer and use it in GitHub Desktop.
TranslateAnimationを動的に設定しやすくするクラスを作ってみました ref: http://qiita.com/TakamiChie/items/8089aa142d033b613d80
public class TranslateAnimationEx extends TranslateAnimation implements
AnimationListener {
private AnimationListener mListener;
private View mView;
private int mFromX;
private int mFromY;
private int mToX;
private int mToY;
public TranslateAnimationEx(View animatedView, int dulation, int toXValue,
int toYValue) {
super(0, toXValue, 0, toYValue);
setDuration(dulation);
mView = animatedView;
mFromX = mView.getLeft();
mFromY = mView.getTop();
mToX = toXValue;
mToY = toYValue;
setAnimationListener(this);
}
public TranslateAnimationEx(View animatedView, int dulation,
int fromXValue, int toXValue, int fromYValue, int toYValue) {
super(fromXValue - animatedView.getLeft(), toXValue
- animatedView.getLeft(), fromYValue - animatedView.getTop(),
toYValue - animatedView.getTop());
setDuration(dulation);
mView = animatedView;
mFromX = fromXValue;
mFromY = fromYValue;
mToX = toXValue - fromXValue;
mToY = toYValue - fromYValue;
setAnimationListener(this);
}
public void animate() {
mView.startAnimation(this);
}
@Override
public void setAnimationListener(AnimationListener listener) {
if (listener.equals(this)) {
super.setAnimationListener(listener);
} else {
mListener = listener;
}
}
@Override
public void onAnimationEnd(Animation animation) {
int x = mFromX + mToX;
int y = mFromY + mToY;
mView.layout(x, y, x + mView.getWidth(), y + mView.getHeight());
mView.setAnimation(null);
mView.setClickable(true);
if (mListener != null) {
mListener.onAnimationEnd(animation);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
if (mListener != null) {
mListener.onAnimationRepeat(animation);
}
}
@Override
public void onAnimationStart(Animation animation) {
mView.setVisibility(View.VISIBLE);
mView.layout(mFromX, mFromY, mFromX + mView.getWidth(),
mFromY + mView.getHeight());
mView.setClickable(false);
if (mListener != null) {
mListener.onAnimationStart(animation);
}
}
}
final ImageView relative_anime = (ImageView) findViewById(R.id.relative_anime);
final ImageView abstract_anime = (ImageView) findViewById(R.id.abstract_anime);
new TranslateAnimationEx(relative_anime, 1000, 10, 10).animate();
new TranslateAnimationEx(abstract_anime, 1000, 20, 30, 20, 30).animate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment