酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

AndroidでObjectAnimatorを使ってViewをアニメーションさせる

久しぶりにAndroidアプリでアニメーションしています。

f:id:ch3cooh393:20161029010642p:plain

説明はあとで書きます。

アニメーションさせる

imageViewのアルファ値を1秒(1,000ミリ秒)かけて0(透明)するアニメーションです。

ObjectAnimator.ofFloat(imageView, "alpha", 0).setDuration(1000).start();

任意のInterpolatorを指定したObjectAnimatorを得る

private ObjectAnimator makeObjectAnimator(final View view, final String property, final float value, final TimeInterpolator interpolator) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, property, value).setDuration(1000);
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            animation.setInterpolator(interpolator);
        }
    });
    return animator;
}

LinearInterpolatorクラスを使う場合です。

makeObjectAnimator(imageView, "alpha", 0, new LinearInterpolator());

複数のアニメーションを同時に実行する

4倍に拡大します。

final List<Animator> animators = new ArrayList<>();
animators.add(makeObjectAnimator(imageView, "scaleX",  4.0f, new LinearInterpolator()));
animators.add(makeObjectAnimator(imageView, "scaleY", 4.0f, new LinearInterpolator()));

final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animators);
return animatorSet;

参考

developer.android.com