LoginSignup
2
3

More than 5 years have passed since last update.

TranslateAnimationを動的に設定しやすくするクラスを作ってみました

Posted at

この間の勉強を元に、TranslateAnimationを拡張して、簡単に「今の位置から任意の位置へ」移動したり、「点Aから点Bへ」移動させるTranslateAnimationExを作ってみました。
また、移動完了後に実際にアニメーション後の位置にビューを移動させたり、移動中はクリックを無効にする機能をつけています。

TranslateAnimationEx.java
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();

アニメーションをリピート再生したときのことは何も考えてないのでsetRepeatCount()で何かの値を指定すると、移動後の位置がおかしくなったりすることがあります。

詳しくはBitbucketにソースコードをあげているのでそちらをご覧あれ。
https://bitbucket.org/TakamiChie/animationex/

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3