2

because animator set doesn't provide REPEAT function, i am implementing my own repetition logic.

but i have a problem on it.

first, let me show you my sample code like below.

My MainActivity ->

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button button;

    private AnimatorSet animatorSet;
    private Repetition repetition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);
        button = (Button) findViewById(R.id.button);

        animatorSet = new AnimatorSet();
        repetition = new Repetition();

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

    private void startAnimation() {
        imageView.post(new Runnable() {
            @Override
            public void run() {
                final ObjectAnimator moveY = ObjectAnimator.ofFloat(imageView, "y", imageView.getY(), imageView.getY() - 200F);
                moveY.setDuration(1000);

                final ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);
                rotation.setDuration(1000);

                repetition.repeat(animatorSet, 10);

                List<Animator> list = new ArrayList<>();

                list.add(moveY);
                list.add(rotation);

                // problem is here, if i run a single animator, repetition class can not get onAnmationEnd callback.
                animatorSet.play(moveY);

                // if i run playSequentially with multiple animators it works properly;
//                animatorSet.playSequentially(list);
                animatorSet.start();
            }
        });
    }

}

My support class for REPETITION ->

it's listening animator's lifecycle and if animation ends it compare the desire animation repeat count with current repeat count in onAnimationEnd callback. if i run a single animator.. onAnimationEnd not get called...

just first time of onAnimationEnd is called..

public class Repetition implements Animator.AnimatorListener {

    private AnimatorSet animatorSet;
    private int repeatCount;

    private int currentRepeatCount;

    public void repeat(AnimatorSet animatorSet, int repeatCount) {
        this.animatorSet = animatorSet;
        this.repeatCount = repeatCount;

        animatorSet.removeAllListeners();
        animatorSet.addListener(this);
    }

    @Override
    public void onAnimationStart(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationStart");
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if(currentRepeatCount < repeatCount) {
            animatorSet.start();
            currentRepeatCount++;
            Log.d("RepeatitionLog", "onAnimationRepeat by repetition");
            Log.d("RepeatitionLog", "currentRepeatCount : " + currentRepeatCount);
        } else {
            Log.d("RepeatitionLog", "onAnimationEnd");
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationCancel");
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationRepeat");
    }
}

I'd appreciate any help!

Thanks.

J.ty
  • 337
  • 1
  • 3
  • 18

0 Answers0