'Android Studio repeatable Fade Animation to ImageView on Button Click
I´m new to programming, started in the Lockdown last year with youtube videos and a book. I´m trying to make a button, that starts an fade out animation on an ImageView. which should be repeatable. It´s basically a dice roll and i want the dice to appear and then disappear every time within like 2 seconds after click.
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
dice.setAnimation(animation);
animation.setStartOffset(3000);
animation.setDuration(100);
dice.setVisibility(View.VISIBLE);
dice.animate().alpha(1).setDuration(500);
btnDice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation1 =AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
dice.setAnimation(animation1);
}
});
this is the content of the fade_out animation:
<alpha
android:duration="2000"
android:fromAlpha="1"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0"
/>
and i´ve got this in the top part:
android:fillAfter="true"
this is my dice, its an array with 6 dice pictures in it:
private void changeDice() {
Random random = new Random();
int randomNumber = random.nextInt(6);
dice.setBackgroundResource(dicee.getResourceId(randomNumber, 1));
}
im not sure how to connect all this to make the button appear and disappear each time. it works only once and since i put the dice image to "gone" added this:
dice.setVisibility(View.VISIBLE);
dice.animate().alpha(1).setDuration(500);
i get some weird results, that i cant explain. first animation works, after that the dice appear, animation wont trigger again and on multiple clicks the dice sometimes disappear.
maybe someone might see what i could change to make it work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
