Start Animation repeatedly every minutes in android app - java

I want my animation start repeatedly every 2 minutes in my android app, as my code it doesn't work. Please help on my code below. Thanks in advance.
TableLayout = (TableLayout) this.findViewById(R.id.table01);
slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
TableLayout.startAnimation(slide_down);
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
TableLayout.startAnimation(slide_up);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Check out repeating animation with Timer
I agree with the accepted answer that a Handler can be used with postDelayed and a Runnable

TableLayout = (TableLayout) this.findViewById(R.id.table01);
slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
TableLayout.startAnimation(slide_down);
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
TableLayout.startAnimation(slide_up);
slide_up.setStartOffset(2000); // delay in ms
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
TableLayout.startAnimation(slide_down);
slide_down.setStartOffset(2000); // delay in ms
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I hope it will work!

Pls use:
TimerTask slideDownTask= new TimerTask() {
public void run() {
TableLayout.startAnimation(slide_down);
});
}};
new Timer().schedule(slideDownTask, 0, 120000);

Related

How to hide textview after animation

I want to click the Button it will show the TextViewis visible(perform slidedown animation), then want to click the button again it will perform another animation(slideup). after that don't need to show the TextView.
How do i fix it?
please anyone have an answer to help me.
bclickss.setOnClickListener(new View.OnClickListener() {
boolean visible;
#Override
public void onClick(View v) {
if( visible = !visible) {
tv2.setVisibility(visible ? View.VISIBLE : View.GONE);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
tv2.startAnimation(anim);
}
else {
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
tv2.startAnimation(anim);
tv2.setVisibility(View.GONE);
}
}
});
use this before startAnimation
if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
tv2.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
tv2.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
tv2.startAnimation(anim);
In second animation you need something like this:
if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
tv2.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
tv2.startAnimation(anim);
try this:
//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) {
tv2.setVisibility(View.GONE);
}
});
You should use clearAnimation before setting the visibility to View.GONE
anim .setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
tv2.clearAnimation();
tv2.setVisibility(View.GONE);
}
});
I also have the same issue. Please follow below lines:
Animation slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
Animation slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
for making view visible :
view.setVisibility(View.VISIBLE);
view.startAnimation(slide_down);
for hiding it:
view.startAnimation(slide_up);
view.setVisibility(View.GONE);
If works please accept the answer.
I have one suggestion for you to use YOYO Animation lib for android.
It provide verious animations along with call back methods so you can hide your textview once your animation get finished in call back
check here

Make something happen when ImageView animation is over

Let's say i have the following code:
img1.animate().alpha(.5f).setDuration(300);
And I want to execute this whenever the animation is over
img1.setImageReasource(R.drawble.img_guitar);
How do I do that??
You can add a listener to your animation:
imageView.animate().setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageView.setImageResource(R.drawable.img_guitar);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
You can also use withActionEnd(Runnable):
imageView.animate().withEndAction(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.img_guitar);
}
})

Detect end of animation (Android Studio)

I would like to know how you detect the end of an animation.
Here is the animation:
image.animate().xBy(screenWidth-200).setDuration(5000).start();
Add an AnimatorListener:
Animator.AnimatorListener listener = new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
image.animate().xBy(screenWidth-200).setDuration(5000).setListener(listener).start();
Are you trying to execute some action after the animation ends? If so try modifying your code like below :
image.animate()
.xBy(screenWidth-200)
.setDuration(5000)
.withEndAction(new Runnable() {
#Override
public void run() {
// Do something when animation ends
}
})
.start();
addListener(AnimatorListener) override method
https://developer.android.com/reference/android/animation/Animator.AnimatorListener.html

on end listener for property animation

This question shows how you set listeners for an android view animation, but it doesn't work for property animations.
How can I achieve the same thing with a property animation?
my animation:
ViewPropertyAnimator viewPropertyAnimator = layout.animate().y(integer).setInterpolator(interpolator).setStartDelay(delay).setDuration(duration);
Try this:
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});

How to clear animation in android?

In my android app I am making a button shake using this code
public void FlashButton() {
final Button button = ((Button)findViewById(R.id.button_message));
final Animation moveright = AnimationUtils.loadAnimation(this, R.anim.moveright);
final Animation moveleft = AnimationUtils.loadAnimation(this, R.anim.moveleft);
final Animation moveright2 = AnimationUtils.loadAnimation(this, R.anim.moveright2);
AnimationListener animation1Listener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
button.startAnimation(moveleft);
}
};
AnimationListener animation2Listener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
button.startAnimation(moveright2);
}
};
AnimationListener animation3Listener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
button.startAnimation(moveright);
}
};
moveright.setAnimationListener(animation1Listener);
moveleft.setAnimationListener(animation2Listener);
moveright2.setAnimationListener(animation3Listener);
button.startAnimation(moveright);
}
This code sets three animations sequentially in a loop forever.
But how do I stop the animation, and make the button normal again?
I tried .clearAnimation(); but it didn't work...
Does anyone know?
Thanks.
Try cancelling the animations and see, something like
moveright.cancel();
moveright2.cancel();
moveleft.cancel();
Or try reset as well
moveright.reset();
similar for others
If you can't solve the issue for whatever reason, I found that starting a new reset type animation can help:
.... <scale
android:duration="1"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"
android:fillAfter="true"
/> ....

Categories