TranslateAnimationを動的に設定しやすくするクラスを作ってみました ref: http://qiita.com/TakamiChie/items/8089aa142d033b613d80
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
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); | |
} | |
} | |
} |
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
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