Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Is there a .TranslationX() change listener for a view

I have a button in my layout. And I am animating the position of that button using ObjectAnimator with translationX animation.

ObjectAnimator btnAnimator = ObjectAnimator.ofFloat(myBtn, "translationX", 
        ViewHelper.getTranslationX(myBtn), 0);

btnAnimator.addListener(new AnimatorListener() {
    @Override
    public void onAnimationCancel(Animator arg0) {}

    @Override
    public void onAnimationEnd(Animator arg0) {
        Log.i("TAG","Animation Finished");
    }

    @Override
    public void onAnimationRepeat(Animator arg0) {}

    @Override
        public void onAnimationStart(Animator arg0) {}
});
btnAnimator.setDuration(animationSpeed).start();

Now I would like to have a listener for the TranslationX of that button to notify whenever the TranslationX position of the button changes.

like image 394
Dileep Perla Avatar asked Oct 01 '22 00:10

Dileep Perla


2 Answers

Here's an easy way I found to do what you're after:

btnAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        Log.e("TAG", "translateX: "+animation.getAnimatedValue("translationX"));
    }
});

btnAnimator.setDuration(animationSpeed).start();
like image 63
Simas Avatar answered Oct 03 '22 07:10

Simas


Two possible approaches:

1) Override onLayout() in your view to manually compare and detect position changes.

2) Use onLayoutChangeListener on your View:

button.addOnLayoutChangeListener(new OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
                    int oldTop, int oldRight, int oldBottom) {
        // Check your new position vs the old one
    }
});
like image 25
Jitsu Avatar answered Oct 03 '22 06:10

Jitsu