I'm trying to cancel an animation with a very simply way just to see whether if .cancel()
function doing what it is intended to do.
But it seems it just does not work.
Here is the code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.my_text_view);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_animation);
textView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
animation.cancel();
}
}, 2000);
}
}
The animation is running forever. It just doesn't stop after 2 seconds.
If I call it from the view:
textView.getAnimation().cancel();
It doesn't work either.
What do you think what makes this? I need a proper way to stop an animation.