AndroidStudioでアニメーション

アニメーションで紙吹雪を降らせる方法を考えてみました

紙吹雪を降らせる方法はたくさんあると思いますが、なるべく毎回ランダムで降らせたいなと言うことで私なりに調べてみました。

アニメーションクラス

public class Animation {

    static Random random = new Random();
    static ObjectAnimator transY, scaleX, transX;


    static List<Animator> animatorList = new ArrayList<>();

    //紙吹雪が下に落ちる時間をランダムで生成 5 ~ 10 秒
    public static int duration() {
        int num = random.nextInt(6) + 5;
        return num;
    }

    //イメージViewにアニメーションをセット
    public static void kamifubukiAnimation(final ImageView image) {

        animatorList.clear();
        int animationNumber = random.nextInt(4);
        int scaleJudge = random.nextInt(3);     //回転の判定を行う変数
        int transXJudge = random.nextInt(3);    //移動の判定を行う変数
        int duration = duration();

        //下に落ちる処理
        transY = ObjectAnimator.ofFloat(image, "translationY", 0.0f - 75.0f, 1920.0f);
        transY.setDuration(duration * 1000);
        animatorList.add(transY);

        //回転があるか判定
        if (scaleJudge >= 1) {
            scaleX = ObjectAnimator.ofFloat(image, "scaleX", 0.1f, 1.0f, 0.1f, 1.0f, 0.1f, 1.0f, 0.1f, 1.0f);
            scaleX.setDuration(duration * 1000);
            animatorList.add(scaleX);
        }
        //移動があるか判定
        if (transXJudge == 1) {
            // 右から左
            transX = ObjectAnimator.ofFloat(image, "translationX", image.getX(), image.getX() + 100.0f, image.getX() - 100.0f);
            transX.setDuration(duration * 1000);
            animatorList.add(transX);
        } else if (transXJudge == 2) {
            // 左から右
            transX = ObjectAnimator.ofFloat(image, "translationX", image.getX(), image.getX() - 100.0f, image.getX() + 100.0f);
            transX.setDuration(duration * 1000);
            animatorList.add(transX);
        }

        //アニメーションの同時再生
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animatorList);

        transY.addListener(new Animator.AnimatorListener() {
            //アニメーション開始時
            @Override
            public void onAnimationStart(Animator animation) {}
            //アニメーション終了時
            @Override
            public void onAnimationEnd(Animator animation) {
                image.setImageBitmap(null);
                image.setImageDrawable(null);
                System.gc();
            }
            //アニメーションキャンセル時
            @Override
            public void onAnimationCancel(Animator animation) {}
            //アニメーションリピート時
            @Override
            public void onAnimationRepeat(Animator animation) {}
        });
        set.start();
    }
}

複数のアニメーションをImageViewに同時にセットします。

ImageViewに格納させる要素が毎回違うので不規則な動きを演出できるんじゃないかな?と思います。

まず、下に落ちる処理で何秒で上から下に落ちる時間が決まります。

次は回転させる処理ですね、でも実際には widthを縮めたり伸ばしたりしてクルクル回っているように見せかけているだけですw

最後は移動させてユラユラ動く感じを演出しています。

これをImageViewにセットすると下の画像のようになります。

ImageViewはTimerクラスで定期的に出現させています、画像は画面外にどんどん溜まっていくので画面外に出たら消えるように処理しないとリークが起こるんで注意してください。

記事のサブ画像




コメント

コメントを追加

user-symbol

Stay in touch

ビジネスおよび開発者向けの実用的な最新情報をご希望ですか?

ソースコードプロジェクトに対するPieceXコミュニティのニーズについてご提供します。

PieceXの最新の無料コミュニティコードプロジェクトをいち早くお知らせします。