Reputation: 81
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?
Upvotes: 7
Views: 8205
Reputation: 8112
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()
Upvotes: 0
Reputation: 449
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) {
}
});
Upvotes: 3
Reputation: 4646
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){
}
Upvotes: 8