I have a fadeout animation in a view (which is inside a fragment), and everytime the animation happens, after it finishes the view redraws itself again. I found a work around doing view.SetVisibility(View.GONE) . But it doesn't wait for the animation to finish. I would like to execute this setVisibility code only after the animation has finished. What is the best way to do that?
You can add Animation listener to your animation object like
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
}
});
Functionally the same as the accepted answer but in a much more concise way:
// Add/Remove any animation parameter
theView.animate()
.alpha(0)
.setDuration(2000)
.withEndAction(new Runnable() {
#Override
public void run() {
theView.setVisibility(View.GONE);
}
});
Enjoy :)
Simply take your animation object and add animation listener to it.
Here is the example code :
rotateAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
**// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**
}
});
You can also achieve this using Animation.setFillAfter
Example for Kotlin
var fadeOutImage = findViewById<ImageView>(R.id.fade_out_Image)
val fadeOutAnimation = R.anim.fade_out_animation
val animation = AnimationUtils.loadAnimation(this, fadeOutAnimation)
fadeOutImage.startAnimation(animation)
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(p0: Animation?) {
// not implemented
}
override fun onAnimationRepeat(p0: Animation?) {
// not implemented
}
override fun onAnimationEnd(p0: Animation?) {
fadeOutImage.visibility = View.INVISIBLE
}
})
Related
I made an animation (ObjectAnimator)for a text view it work correctly but i have a question.
I wanna know is there any method that i can use immediately after animation duration finished ?
(I mean a method like onFinish when we use CountDownTimer)
I want the rest of methods and codes I have in app run when animation finished.Is there a solution for that?
ObjectAnimator deltaXAnimation = ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
ObjectAnimator deltaYAnimation = ObjectAnimator.ofFloat(winMassage,"scaleY",1f,1.5f);
deltaYAnimation.setDuration(300);
deltaYAnimation.start();
HI there are proper callback methods in which you get Animation Start and end Listeners
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
deltaXAnimation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
Below code might be understand well, creating a listener for ObjectAnimator
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation a) {
//
}
public void onAnimationRepeat(Animation a) {
}
public void onAnimationEnd(Animation a) {
}
});
We can go also to your ObjectAnimator
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//stop animation after 5 seconds
deltaXAnimation.cancel();
}
}, 5000); //5000 equals to 5seconds
}
Hope this might help you
I have two functions that animate two different TextViews but they both seem to start at the same time but I am trying to have the animation of the first function finish first and then start the second function.
public Boolean functionFinished = false;
public void runFunction(){
firstFunction();
if(functionFinished = true){
secondFunction();
}
}
public void firstFunction(){
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
initialCount.startAnimation(out);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if(initialCount.getText().equals("3")){
initialCount.setText("2");
initialCount.startAnimation(out);
} else if(initialCount.getText().equals("2")){
initialCount.setText("1");
initialCount.startAnimation(out);
} else if (initialCount.getText().equals("1")){
initialCount.setText("START!");
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
functionFinished = true;
}
The second function simply changes its own textView every second counting up from 0.
What did I do wrong / how do I correct this so that the second function runs after the first function has finished? (ie. sets functionFinished to true only when the TextView from the firstFunction is "START!")
public void runFunction() {
firstFunction();
}
public void firstFunction() {
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
...
secondFunction();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
initialCount.startAnimation(out);
}
Just call second function from onAnimationEnd. Also keep in mind that you should attach listener before start animation.
If your minimum sdk version is 14:-
textView1.animate()
.alpha(0f)
.setDuration(400)
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
// animate second textview here....
}
});
Because you assign functionFinished = true at the end of the firstFunction().
And you must misunderstand that initialCount.startAnimation(out) will block the code and firstFunction() completes only after animation ends .
initialCount.startAnimation(out) will not block any code ,and functionFinished = true is run immediately. so secondFunction() will run immediately after firstFunction() finishes instead of animation finishes.
I've write an Animation for make text of a TextView blink, and i would perform an action after animation ends.
It's possible to intercept end of animation in some way?
AnimationListener listener = new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// Do your stuff
}
};
I am using NineOldAndroid's AnimatorSet on a View
and when i want to get rid of the animation, and make the view gone - nothing happens
how do i make the view go away?
public void showAnimation(boolean show) {
if (show) {
if (mTarget.getVisibility() != View.VISIBLE) {
// play sound
mTarget.setVisibility(View.VISIBLE);
pauseAnimation = false;
// start animation
initTargetAnimation();
}
} else {
pauseAnimation = true; //All of this block gets executed in the debugger, but none of it actually take effect
if (mTarget.getAnimation() != null) {
mTarget.getAnimation().cancel();
mTarget.clearAnimation();
}
mTarget.setVisibility(View.GONE);
}
}
private void initTargetAnimation() {
final AnimatorSet set = new AnimatorSet();
// init animation properties
set.playTogether(ObjectAnimator.ofFloat(mTarget, "scaleX", RELEVANT_ANIMATION_SEQUENCE), ObjectAnimator.ofFloat(mTarget, "scaleY", RELEVANT_ANIMATION_SEQUENCE));
// add animation listener
set.setDuration(1500).addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
// RESTART ANIMATION
if (!pauseAnimation) {
initTargetAnimation(); // TODO fix
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
// start animation
set.start();
}
After you set the visibility on your View mTarget try calling invalidate() on it. This assumes you call it from the main thread otherwise it's postinvalidate().
Here is the situation. My Activity is running, and I call a function to start an animation. Once this animation finishes, I need to wait for another 2 seconds, then continue the process normally. What do you suggest I do?
The OnAnimationEnd does not help my case by the time I reach the end of iteration, my process has already returned from the calling function and continued processing.
What do you think I do? Do thread.sleep (duration of animation + 2 sec)?
Thanks
EDIT:
OK I thought maybe I should explain what I am doing in code
I have the following code
Public class myclassActivity extends Activity{
..
.
.
.
public void runGame(){
player1.startAnimating();
player2.start Animating();
player3.startAnimating();
updateStatus();
runGame();
}
}
So as you can see my activity keeps calling startAnimating for each player. In which some work and then animation for 1 second is done.
I don't want player2.startAnimation to start untill the player1.startAnimation is completed and so on for the rest of the code.
Currently what happens is that the code executes fully and rerun again while the animation is still running. Pleasee any help on this?
observe this code it is help full you.
public class PlayersActivity extends Activity {
ImageView I1,I2;
Button B;
Animation T1,T2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
I1=(ImageView)findViewById(R.id.i1);
I2=(ImageView)findViewById(R.id.i2);
B=(Button)findViewById(R.id.b1);
B.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
T1=new TranslateAnimation(0,100,0,0);
T1.setDuration(1000);
I1.startAnimation(T1);
T1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
T2=new TranslateAnimation(0,100,0,0);
T2.setDuration(1000);
T2.setStartOffset(2000);
I2.startAnimation(T2);
T2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
T1.setStartOffset(2000);
I1.startAnimation(T1);
}
});
}
});
}
});
}
}
Suppose you will start this two methods after completed player3, you are placing player3 onAnimationEnd(){ updateStatus(); runGame();}
Use postDelayed function to run something after exact delay. For example (2 sec delay):
LinearLayout.postDelayed(new Runnable() {
public void run() {
//Do something
}
}, 2000);
how about passing caller instance by
player1.startAnimating(this)
and then call method like
myclassActivity#animationFinished(this)
on finishing animation of player1/2? then
void animationFinished(Player p) {
if (p==player1) {
player2.start();
} else if (p==player2) {
player3.start();
}
}
or something