Aboudi Aboudi - 1 year ago 57
Android Question

How can I interact with animations?

I have an animation that slides across the screen, while the animation is playing I want it to disapear if it has been clicked!

I have tried to use OnClickListener() and also OnClickMethods, both only work after the animation has finished playing!

TranslateAnimation animation = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {

public void onAnimationRepeat(Animation animation) {


public void onAnimationEnd(Animation animation) {
if (AdPlayer0.isPlaying()) {

answer = rn.nextInt(width) + 1; //useless
TranslateAnimation animation0 = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to


I was reading the answer here and something interesting was mentioned:

Animations do NOT effect the 'physical' location of the views on screen. If you want it to actually move then you need to adjust the properties of the view.

I have now tested the above quote, and it seems to be true! I have also looked into how to adjust the position properties of the view after the animation has ended, however I want to change its position before an animation has ended(While its playing)!

Any suggestions for a work around? ideally I would want to change the physical position of the view while the animation is played!

Answer Source

A TranslationAnimation only moves the pixels on the screen, so an OnClickListener does not animate with it. Use ViewPropertyAnimator instead and set an OnClickListener then it should work.

The code would look something like this :

Floater1.animate().setDuration(5000).translationX(answer).translationY(height * -1);

Check out all the available methods in the docs.

Edit/Update :

.translationX(float toX)

means -> animate the view to this point on the x axis.

The animation always starts at the current position of the view, so you don't need a fromX. There are also other methods which you can use :

 .translationXBy(float byX)
 .translationYBy(float byY)

With these you can translate the view BY the float value, and not TO a certain point on the screen.