I have a series of animations that I call in series like so:
final Animation anim1 = AnimationUtils.loadAnimation(thisObject, R.anim.animation1);
final Animation anim2 = AnimationUtils.loadAnimation(thisObject, R.anim.animation2);
final Animation anim3 = AnimationUtils.loadAnimation(thisObject, R.anim.animation3);
final Animation anim4 = AnimationUtils.loadAnimation(thisObject, R.anim.animation4);
anim1.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation animation) { }
public void onAnimationRepeat(Animation animation) { }
public void onAnimationEnd(Animation animation)
{
image.startAnimation(anim2);
}
});
anim2.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation animation) { }
public void onAnimationRepeat(Animation animation) { }
public void onAnimationEnd(Animation animation)
{
image.startAnimation(anim3);
}
});
// etc...
view.startAnimation(anim1);
While this works, the image snaps back to its original position for a split second between each animation. I've defined android:fillAfter as true in all my animation XML files, but this doesn't seem to change anything. Is there a way to prevent this snap between animations? Or is there a better way to achieve the same effect?
Related
How can I make a e.g. Fade out animation for a floating view that don't has a parent layout. This code works fine for everything nested inside layouts. Is there an option to make this possible for parents which are displayed in a floating service?
Code:
protected void FadeAnimation(View animationview, boolean fadein)
{
if(fadein){
//Fade in Animation
Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.introfadein);
animationFadeIn.setFillAfter(true);
animationFadeIn.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
animationview.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
animationview.setAnimation(animationFadeIn);
} else {
//Fade out Animation
Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.introfadeout);
animationFadeOut.setFillAfter(true);
animationFadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
animationview.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
animationview.setAnimation(animationFadeOut);
}
}
Thanks for your help! :)
With the code below, the TextView variable tvAppName slides up successfully, but after 1,200 milliseconds the view goes back to its position. How can I fix the view after moving it?
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
TranslateAnimation anim = new TranslateAnimation(0, 0, 100, 0);
anim.setDuration(500);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tvAppName.startAnimation(anim);
}
}, 1200);
}
From the documentation: You need use setFillAfter method.
anim.setFillAfter( true );
If fillAfter is true, the transformation that this animation performed
will persist when it is finished
Hope it helps.
You should use anim.setfillafter(true)
the transformation that this animation performed will persist when it
is finished
If you did not use code below.you can Delete That
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
You should use NineOldAndroid library. This is a really good library. It will help you with a lot of animations.
i'd like to make a translateAnimation in my Android app with the code below :
TranslateAnimation anim = new TranslateAnimation(0,0,-400,0);
anim.setDuration(400);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mlLinearLayout.clearAnimation();
mlLinearLayout.requestLayout();
mlLinearLayout.layout(mlLinearLayout.getLeft(), mlLinearLayout.getTop()+400, mlLinearLayout.getRight(), mlLinearLayout.getBottom());
}
});
anim.setInterpolator(new AccelerateInterpolator());
anim.setFillEnabled(true);
anim.setFillAfter(true);
anim.setFillBefore(false);
mlLinearLayout.startAnimation(anim);
active=false;
but when this animation was done, the LinearLayout back to his start place even if I rebuild my view with the new position. How can I change it please ?
I finally found solution :
TranslateAnimation anim = new TranslateAnimation(0,0,0,400);
anim.setDuration(400);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mlLinearLayout.layout(mlLinearLayout.getLeft(), mlLinearLayout.getTop()+400, mlLinearLayout.getRight(), mlLinearLayout.getBottom());
}
});
anim.setFillEnabled(true);
anim.setFillAfter(false);
anim.setFillBefore(false);
mlLinearLayout.startAnimation(anim);
active=false;
As i rebuild my view after translation, fillafter isn't usefull, Now it's working perfect without lag and jump !
I am animating a view using translate animation. I've used setFillAfter(true) and it animates successfully.
My code is.
final ImageView image= (ImageView) findViewById(R.id.image2);
image.setBackgroundResource(R.drawable.lamb);
final Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
animation.setFillAfter(true);
image.startAnimation(animation);
animation.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation animation) { }
public void onAnimationRepeat(Animation animation) { }
public void onAnimationEnd(Animation animation)
{
Handler ss = new Handler();
ss.postDelayed(new Runnable()
{
public void run()
{
image.setVisibility(View.INVISIBLE);
}
} ,4000);
}
});
I use a Handler for hiding the image view after 4 seconds, but it does not hide. How can I hide the image after some time?
After the animation ends, I want to show the image on the last position that it was so I use setFillAfter(true); after 4 seconds I hide the image view.
How can I make the image invisible?
You will need to set AnimationListener. And when animation will end, make the view invisible.
moveForward.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
image.clearAniamtion(), before setVisibility(View.INVISIBLE)
I would like to make a simple animation. My logo is divided in 4 parts :
1|2
3|4
I would like to fadein-out each part, but only one after the other : 1 -> 2 -> 4 -> 3 -> 1...
I have no problem with this, but sometimes my animation is not synchronized and the 4 parts animate simultaneously :
final ImageView logo1 = (ImageView) v.findViewById(R.id.logo1);
logo1.clearAnimation();
final ImageView logo2 = (ImageView) v.findViewById(R.id.logo2);
logo2.clearAnimation();
final ImageView logo3 = (ImageView) v.findViewById(R.id.logo3);
logo3.clearAnimation();
final ImageView logo4 = (ImageView) v.findViewById(R.id.logo4);
logo4.clearAnimation();
final Animation anim1 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim1.setFillAfter(true);
anim1.setStartOffset(0);
final Animation anim2 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim2.setStartOffset(anim1.getDuration());
anim2.setFillAfter(true);
final Animation anim3 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim3.setStartOffset(anim1.getDuration()+anim2.getDuration());
anim3.setFillAfter(true);
final Animation anim4 = AnimationUtils.loadAnimation(c, R.anim.animlogo);
anim4.setStartOffset(anim1.getDuration()+anim2.getDuration()+anim3.getDuration());
anim4.setFillAfter(true);
anim1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim1.setStartOffset(anim2.getDuration()+anim4.getDuration()+anim3.getDuration());
logo1.startAnimation(anim1);
}
});
anim2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim2.setStartOffset(anim1.getDuration()+anim4.getDuration()+anim3.getDuration());
logo2.startAnimation(anim2);
}
});
anim3.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim3.setStartOffset(anim2.getDuration()+anim4.getDuration()+anim1.getDuration());
logo4.startAnimation(anim3);
}
});
anim4.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
anim4.setStartOffset(anim2.getDuration()+anim1.getDuration()+anim3.getDuration());
logo3.startAnimation(anim4);
}
});
logo1.startAnimation(anim1);
logo2.startAnimation(anim2);
logo4.startAnimation(anim3);
logo3.startAnimation(anim4);
anim1.startNow();anim2.startNow();anim3.startNow();anim4.startNow();
Any idea ?
Start only anim1:
logo1.startAnimation(anim1);
and in anim1's AnimationListener's onAnimationEnd do:
logo2.startAnimation(anim2);
This way anim2 won't start before anim1 is finished.