how can i translate the animation of an android button?

This happens because the animation is performed by changing the translationX and translationY view properties, not the actual position which the ui framework authors, in their infinitesimal wisdom, have placed in an external class, inheriting LayoutParams. Consequently the click events are dispatched to the “raw” position of the view which does not account for the view transformations (reference). Animations in android are still pretty tedious and untamed but at least there are many approaches to take. For this case a ValueAnimator for the LayoutParams fields seems like a solution. If the layout allows margins then it can look like this:

final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) btnNext.getLayoutParams();
int initialX = displayWidth - btnNextWidth, finalX = displayWidth / 2;

ValueAnimator.ofInt(initialX, finalX)
    .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         public void onAnimationUpdate(ValueAnimator valueAnimator) {
             Integer ax = (Integer) valueAnimator.getAnimatedValue();
             Log.i("anim", "x: " + ax);
             lp.leftMargin = ax;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top