1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidのアニメーション(ボール落下)

Posted at

アニメーション効果

ボール落下アニメーション効果.gif

1、アニメーションの設定定数、時間と回数とモード

//時間
private final int DURATION_TIME = 2000;
//回数
private final int REPEAT_COUNT = Animation.INFINITE;
//2回以降の開始位置、
//RESTART:初回の開始位置
//REVERSE:終了位置
private final int REPEAT_MODE = Animation.RESTART; //REVERSE RESTART

2、アニメーションの初期化

//大きさアニメーション
private ScaleAnimation scaleAnimation;
{
    scaleAnimation = new ScaleAnimation(
            1f,0.5f,                                 //Xの開始大きさ
            1f,0.5f,                                 //Yの開始大きさ
            Animation.RELATIVE_TO_SELF,0.5f,       //開始位置の位置X
            Animation.RELATIVE_TO_SELF,0.5f        //開始位置の位置Y
    );
    scaleAnimation.setDuration(DURATION_TIME);
    scaleAnimation.setRepeatCount(REPEAT_COUNT);
    scaleAnimation.setRepeatMode(REPEAT_MODE);
}

//移動アニメーション初期化
private TranslateAnimation translateAnimation;
{
    //Animation.RELATIVE_TO_PARENT  親のViewに対して
    //Animation.RELATIVE_TO_SELF      自分のViewに対して
    translateAnimation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,0, //"X"開始位置のタイプとValue
            Animation.RELATIVE_TO_PARENT,0f, //"X"終了位置のタイプとValue
            Animation.RELATIVE_TO_PARENT,0, // "Y"開始位置のタイプとValue 
            Animation.RELATIVE_TO_PARENT,0.9f // "Y"終了位置のタイプとValue
    );
    //アニメーションの時間
    translateAnimation.setDuration(DURATION_TIME);
    //アニメーションの回数数字または Animation.INFINITE(無限回)
    translateAnimation.setRepeatCount(REPEAT_COUNT);
    //アニメーションのMode
    translateAnimation.setRepeatMode(REPEAT_MODE);
}

3、アニメーションセットの設定

    //ボール跳ね返す効果
    animationSet.setInterpolator(new BounceInterpolator());
    //アニメーション設定
    animationSet.addAnimation(scaleAnimation);
    animationSet.addAnimation(translateAnimation);

4、アニメーション開始

    btn_start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img_ball.startAnimation(animationSet);
        }
    });

ソースコード

いいねも忘れずに:point_up:

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?