user8222336
user8222336

Reputation: 11

Animation moves just on first button click

I have one question. I have a button ant when I click on it, imageView (with objectAnimator) goes down. I have my ˝animation down˝ in onClick and every thing is very simple, I click on the button and image goes down - there I have a question.

First, there is my code:

ObjectAnimator objectAnimatorBlock1 = getDownObjectAnimator(imageBlock1);
    animatorSetBlock1 = new AnimatorSet();
    animatorSetBlock1.play(objectAnimatorBlock1);
    animatorSetBlock1.start();

private ObjectAnimator getDownObjectAnimator(View v) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v, "translationY", 0.0f, 860.0f);
        objectAnimator.setDuration(2000);
        return objectAnimator;

and XML:


<ImageView
        android:contentDescription="@string/image_description"
        android:layout_marginRight="202dp"
        android:layout_marginEnd="202dp"
        android:layout_marginTop="-155dp"
        android:layout_width="200dp"
        android:layout_height="90dp"
        android:id="@+id/imageBlock1"
        android:layout_gravity="end|center_vertical"
        android:src="@drawable/distance11" />
<Button
    android:layout_width="60dp"
    android:onClick="countIN"
    android:layout_marginLeft="20dp"
    android:layout_height="61dp"
    android:text="DROP THE BLOCK"
    android:id="@+id/button2"
    android:layout_gravity="start|center_vertical"
    android:background="@drawable/layout100"
    android:textStyle="normal|bold|italic" />

My problem is: First time when I click on the button, image goes down and there also stand still(that is correct), but I would like that image goes down JUST when I first time click on a button. After first click animation is clear(finish animation) - then on second, third, fourth,... click , there is not any effect on image(it do not move). How can I do these?

Upvotes: 1

Views: 46

Answers (1)

Govind Mohan
Govind Mohan

Reputation: 111

You can use AnimationUtils instead of ObjectAnimator. You can write your animation in xml and store it in the R.anim folder. Then, you can apply it to the button's onClick listener as follows:

//image is your ImageView
button.setOnClickListener( 

...

//Suppose your animation is saved as moveDown.xml
Animation moveDown = AnimationUtils.loadAnimation(getApplicationContext, R.anim.
image.startAnimation(moveDown);

...

});

This will make your ImageView (image) perform the animation each time you click the button. I hope this helps you out.

Upvotes: 1

Related Questions