1

I want to set a touch event on an animated view and then stop the animation on touching view.

Here is my activity code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       touch_me = (TextView) findViewById(R.id.touch_me);
       touch_me.setOnTouchListener(new OnTouchMeListener());
       Display display = getWindowManager().getDefaultDisplay();
       Point size = new Point();
       display.getSize(size);
       int fromX = size.x;
       int fromY = size.y;
       int toX = (-1) * view.getWidth();
       TranslateAnimation animation = new TranslateAnimation(fromX, -100, 0, 0);
       animation.setDuration(6000);
       animation.setRepeatCount(5);
       animation.setFillAfter(true);
       touch_me.startAnimation(animation);
   }

Here is my class listener:

public class OnTouchMeListener implements View.OnTouchListener{

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        touch_me.clearAnimation();
        return true;
    }
}

The problem is that the onTouch is detected only on the initial position(in XML file) of the view Can anyone help me please ?

Lionel Messi
  • 395
  • 1
  • 4
  • 10

1 Answers1

0

ok, here's the problem.

the view animation you are using is from older system and it only simulates the animation, not move the positioning of the view. so when you touch the area where the view has moved, there is no reading on your touch so it gets ignored. but if you touch the area of the original position of the view, it will fire the touchevent callback method and cancel the animation per your code.

there are several approach to solve this.

one is this by calling onlayout and relocating the view each time there is a change in the animation by calling the view's onlayout method. Button is not clickable after TranslateAnimation

or another is to change the view's touch area by using touchdelegate class http://cyrilmottier.com/2012/02/16/listview-tips-tricks-5-enlarged-touchable-areas/

but i think these two options aren't very good approaches to your problem.

i would go with this third option: use propertyanimation instead. it's different from older system in that it updates the values needed for changes instead of doing the actual animation. the actual animation is done inside the update method with your own algorithm.

http://developer.android.com/guide/topics/graphics/prop-animation.html

here is example:

    ValueAnimator animTranslation = ValueAnimator.ofFloat(valueStart, valueEnd);
    animTranslation.setDuration(300);
    animTranslation.removeAllUpdateListeners();
    animTranslation.removeAllListeners();
    animTranslation.addUpdateListener(new     ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {

          myView.setRotation(currValue - (Float) animation.getAnimatedValue());


            }

        }


    });
    animRotation2.addListener(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) {


        }
    });
Community
  • 1
  • 1
JungJoo
  • 171
  • 1
  • 2
  • 10
  • I don't know how to implemente your example in my case. Can you explain me what is animRotation2 and currValue ? – Lionel Messi Nov 22 '15 at 22:18
  • oops. just replace animRotation with animTranslation and you can ignore currvalue. you get the update value with animation.getanimatedvalue – JungJoo Nov 22 '15 at 22:50