vicolored
vicolored

Reputation: 835

Add delay before repeat animation with valueAnimator

I'm trying a simple example like having a TextView and translate it from 0% to 100% of the screen width

code is :

    anim0  = ObjectAnimator.ofFloat(textViewId00, "translationX", -120f, 480f);

where 480f is the width of my screen.

What I want is the next N times ( n=5 for my example ) to add a delay before it starts.

I have tried to add the delay with setStartDelay() method on onAnimationRepeat but there is no effect.

My code is :

        textViewId00 = (TextView) findViewById(R.id.textViewId00);


    anim0  = ObjectAnimator.ofFloat(textViewId00, "translationX", -120f, 480f);
    anim0.setDuration(2000);
    anim0.setRepeatCount(5);
    anim0.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            Log.i(TAG, "anim0 onAnimationStart ");
            textViewId00.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Log.i(TAG, "anim0 onAnimationEND " );
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            Log.i(TAG, "anim0 onAnimation REPEAT " );
            anim0.setStartDelay(14000);
        }
    });
    anim0.start();

The textView appears to the screen, moves until it disappears and then again from start without any delay.

Upvotes: 4

Views: 12916

Answers (1)

daentech
daentech

Reputation: 1115

I have just tried it using this code and it looks like it is doing what you want it to:

public void animateView() {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    final int screenWidth = size.x;

    final ObjectAnimator anim0  = ObjectAnimator.ofFloat(textviewId00, "translationX", -120f, screenWidth);
    anim0.setDuration(2000);
    anim0.addListener(new Animator.AnimatorListener() {

        int count;

        @Override
        public void onAnimationStart(Animator animation) {
            textViewId00.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (++count < 5) {
                anim0.setStartDelay(14000);
                anim0.start();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {}

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });
    anim0.start();
}

Upvotes: 6

Related Questions