Differenciate 2 animations in AnimationListener - java

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) {
}
}

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

Setting up an AnimationListener for an ObjectAnimator

In my program, I have an ObjectAnimator which moves an ImageView from left to right. I am trying to set up a listener which will execute a task when the ObjectAnimator is finished running. Here is the relevant section of code which I am currently using to try to accomplish this:
if (num == 350) {
nAnim = ObjectAnimator.ofFloat(gamePiece, "translationX", 0, num);
nAnim.setDuration(2125);
nAnim.start();
nAnim.addListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animator a) {
startGame(level);
}
#Override
public void onAnimationStart(Animator a) {
}
#Override
public void onAnimationCancel(Animator a) {
}
#Override
public void onAnimationRepeat(Animator a) {
}
});
When I try to run this in Android Studio, I am getting the error: MainActivity is not abstract and does not override abstract method onAnimationStart() in MainActivity. What do I have to do to fix this error?
Since you implemented AnimatorListener in your MainActivity, you must include all its abstract methods, and change nAnim.addListener(new Animat.... to nAnim.addListener(this)
#Override
public void onAnimationStart(Animator animation){
}
#Override
public void onAnimationEnd(Animator animation){
startGame(level)
}
#Override
public void onAnimationRepeat(Animator animation){
}
#Override
public void onAnimationCancel(Animator animation){
}
You should use AnimatorListener class instead of AnimationListener like below
nAnim.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) {
}
});
Or switch to Kotlin and put animator.start() as the last line. Offcourse you don't have to implement all animations methods
val gamePiece = Button(this)
var num = 0
val animator = ObjectAnimator.ofFloat(gamePiece, View.TRANSLATION_X, 0f, num.toFloat())
animator.duration = 2125
if (num ==350){
animator.addListener(object : AnimatorListenerAdapter(){
override fun onAnimationStart(animation: Animator?) {
super.onAnimationStart(animation)
}
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
startGame(level)
}
})
}
//animator.start() should be the last line
animator.start()

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) {
}
});

Categories