setAnimation not always working why? - android

Building this app, I have managed to use some animations
using them with View view.setAnimation() etc..
This is the code i have:
// animation Properties
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(5000);
AnimationSet animation1 = new AnimationSet(false); // change to false
//animation.addAnimation(fadeIn);
animation1.addAnimation(fadeIn);
animation1.setRepeatCount(1);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
//fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(5000);
AnimationSet animation = new AnimationSet(false); // change to false
//animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
textViewTopBannerBizName.setAnimation(animation1);
textViewTopBannerBizCategory.setAnimation(animation1);
So all, I want is that textViewTopBannerBizName and textViewTopBannerBizCategory will fade into screen as i used animation1 for both of them.
However first time when i launched the app it worked perfect but when I relaunched it again it stopped working.
It makes me wonder... why...?
Please help,
Thank you for your time.

try this.(Tested)
textViewTopBannerBizName.startAnimation(animation1);
You can clear animation if cached before by calling clearAnimation() and then startAnimation or setAnimation.
Edited
setAnimation
Sets the next animation to play for this view.But view animation does not start yet.
startAnimation
If you want the animation to play immediately, use startAnimation. This method provides allows fine-grained control over the start time and invalidation, but you must make sure that
1) the animation has a start time set,
2) the view will be invalidated when the animation is supposed to start

After setting animation, Please do invalidate the view if it's not an Activity
imb6.setAnimation(MainActivity.blinkAnimation(mContext, true))
invalidate();
I wish this'll helps.

Related

Why alpha animation not working in my android code?

I want to merge animations in android application.
I'm using below code for that,but alpha animation not wrking.
I have no idea why it is not working.
// create set of animations
AnimationSet login_page_animation = new AnimationSet(false);
// animations should be applied on the finish line
login_page_animation.setFillAfter(true);
// create scale animation
int white_background_height=((TextView) findViewById(R.id.login_white_backgroud)).getHeight();
TranslateAnimation translate_animation = new TranslateAnimation(0,0,0,- white_background_height/4);
translate_animation.setDuration(700);
// create Alpha animation
AlphaAnimation alpha_animation=new AlphaAnimation(0.0f,1.0f);
alpha_animation.setDuration(700);
// add new animations to the set
login_page_animation.addAnimation(translate_animation);
login_page_animation.addAnimation(translate_animation);
Looks like you're adding translate_animation again on that last line instad of alpha_animation.

Android: How to avoid restart after Animation finish?

the image will go back to 0,0 after animation finish
how to set not go back?
still stay in 100,100
Animation am = new TranslateAnimation((float)0(), (float)100, (float)0,(float)100);
am.setDuration(5000);
am.setRepeatCount(0);
point.startAnimation(am);
Use Animation.setFillAfter(true) to persist the final animation state.
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set. Note that this applies to individual animations and when using an AnimationSet to chain animations.
Animation am = new TranslateAnimation((float)0(), (float)100, (float)0,(float)100);
am.setDuration(5000);
am.setFillAfter(true);
am.setRepeatCount(0);
point.startAnimation(am);

Setting end values without animation with AnimatorSet

According to the documentation http://developer.android.com/reference/android/animation/AnimatorSet.html#end() end() method should assign the end values for all animations inside the set. However ending does nothing if the set was not started. This is different from ValueAnimator i.e. which does not care and artifically starts the animation, fires onAnimationStart() event and then ends it which results in setting the end values properly.
Let me tell you why this is a problem. If you want to have the ability to update your View with and without animation (depending on the situation) you don't want to have two separate methods for it. Instead you could create an animation and start it normally or immediately set the end values. With ValueAnimator this works nicely. However AnimatorSet does not play ball and if you try to end it before calling start() it simply exits.
One workaround for this is to call start() and end() immediately after but it does not cover the situation with nested AnimatorSets like this:
AnimatorSet mainSet = new AnimatorSet();
AnimatorSet scaleSet = new AnimatorSet();
scaleSet.playTogether(
ObjectAnimator.ofFloat(view, "scaleX", 0.5f),
ObjectAnimator.ofFloat(view, "scaleY", 0.5f));
mainSet.playSequentially(
ObjectAnimator.ofFloat(view, "alpha", 0.5f),
scaleSet);
mainSet.start();
mainSet.end();
This causes the mainSet to call end() on all its children. But since AnimatorSet.end() is broken the scaleSet does not set ending values. I attempted to extend AnimatorSet class and fix this but soon found out it is marked final.
In my app I mostly use animations but have some few cases where animations are not needed. Does anyone have an idea how to achieve the proper behaviour?
You can do that on all child animations with:
for (Animator animation : mainSet.getChildAnimations())
{
animation.start();
animation.end();
}
I know this is an old question but a recursive approach can solve this:
void endAnimationsRecursively(final Animator mainAnimator){
if (mainAnimator instanceof AnimatorSet) {
for (final Animator animator : ((AnimatorSet)
mainAnimator).getChildAnimations()) {
endAnimationsRecursively(animator);
}
mainAnimator.end();
mainAnimator.cancel();
}
}

Using animation set, animation does not happen sequentially

I am trying to set blink animation on two words so that they blink one after the other, but what ever I do only 2nd word is displayed, can any one provide me method for doing the same, I am working with API level 10 so, cannot use "Animatorset".
AnimationSet set = new AnimationSet( true );
Animation blink = new AlphaAnimation(1, 0 );
blink.setDuration(duration);
blink.setFillAfter(true);
set.addAnimation( blink );
txtvw.setText("FIRST");
txtvw.setVisibility(View.VISIBLE);
txtvw.setAnimation(blink);
AnimationSet set2 = new AnimationSet( true );
Animation blink2 = new AlphaAnimation(1, 0 );
blink2.setDuration(duration);
blink2.setFillAfter(true);
set2.addAnimation( blink );
txtvw.setText("SECOND");
txtvw.setVisibility(View.VISIBLE);
txtvw.setAnimation(blink2);
if your code is like this, your text "first" gets replaced by text "Second" immediately. And the animation is shown. This animation gives an illusion that only second is blinking. Your text first is being set for microseconds but it immediately gets replaced by second.
If you want to show both the texts, you may need to use Thread
Using two animation sets for the same animations (that is, "blinking animation") is an overkill. You may just repeat your animation 2 times. Use setRepeatCount(int) in conjunction with setRepeatMode(int) to achieve this.
If you insist on using the code you have provided in your post, you will need to specify animation offset. Animation offset indicates "how much time should animation wait before start". For example, if you have Animation a and b, and want b happen after a, you might use the code:
b.setStartOffset(a.getDuration());
Then b animation will happen just after a has finished.

Android ObjectAnimation only started once

I use the ObjectAnimator API (android.animation.ObjectAnimator) to animate a button once it's clicked (v is the Button):
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "rotationY", 360f);
animator.setDuration(5000);
animator.start();
When I test this on the emulator, it works for the first click (button rotates). But when I click the button again (the fragment is not destroyed etc. after the first click), I don't see any animation on the emulator (the emulator isn't the fastest, but with 5 seconds I should see something).
Do I need to destroy/close something after the first animation or what am I missing?
Does anyone have a hint or can reproduce this?
Thanks in advance,
Martin
The second time you will try to animate from 360.0f to 360.0f. Change your call to ofFloat() to:
ObjectAnimator.ofFloat(v, "rotationY", 0.0f, 360.0f)
To elaborate on Romain's answer, the result of the single-value factory constructor is animation that will run from whatever the current value is to the value specified in the parameters. In your case, the object had a value of 0 to begin with an animated (the first time) to a value of 360. The second time it ran, it animated from the current value (360) to the specified value (360). Not much of an animation.
The fix is as above: hard-code both the start and end values for the animator. Alternatively, you can reset the value back to 0 when the animation finishes by implementing the AnimatorListener.onAnimationEnd method and resetting it when the animation finishes:
animator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
v.setRotationY(0);
}
});

Categories

Resources