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);
}
})
Related
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
I'd like to know how to differentiate two animations with the same duration in the AnimationListener. I know I can just declare:
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
And then I can do the same with animation 2 resulting in individual animation listeners, however I want something like this (not exactly because Animations do not have ID) mind onAnimationEnd please:
public class example implements Animation.AnimationListener{
#Override
public void onBackPressed() {
if(detallesvis) {
lldetalles.startAnimation(disappear);
}
else{
finish();
}
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (animation.getId()==animation1.getId())/* please note that animation.getId() function doesn't exist, it is just an example */ {
dostuff();
}
if (animation.getId()==animation2.getId()){
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
I solved this problem by setting different durations to each animation and comparing animation.getduration()... however I'd like a cleaner approach (if it is possible).
Equals Animation objects
#Override
public void onAnimationEnd(Animation animation) {
if (animation == animation1) {
dostuff();
}
if (animation == animation2) {
}
}
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
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);
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) {
}
});