How to do translate animation of button in Android Studio?

dimioLt

I am trying to move a button from one site to another. I manage to move it but when it moves, I have to click on the place where it was before instead of hitting the button. Why does this happen?

This is how it looks. You can see how at the end when I click on another side, the button's shadow is activated

My code:

Animation xaml

move.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    >

    <translate
        android:fromYDelta="0%p"
        android:fromXDelta="0%p"
        android:toXDelta="-300"
        android:toYDelta="-300"
        android:duration="500"
        ></translate>

</set>

In the code:

    Animation animation = AnimationUtils.loadAnimation(this,R.anim.move);
    btnNext.startAnimation(animation);
Crispert

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)
    .setDuration(500)
    .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator valueAnimator) {
             Integer ax = (Integer) valueAnimator.getAnimatedValue();
             Log.i("anim", "x: " + ax);
             lp.leftMargin = ax;
             btnNext.requestLayout();
         }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Studio: Toggle button to have the button click animation?

Android Studio: Toggle button to have the button click animation?

How do I change the state of a switch in Android studio via a button?

How do you make an invisible button that works (in android studio)

Android Fragment Translate Animation Not Working

Android Translate Animation not working properly

How to do on Button click animation Like in Google android application back Button?

How do I get Android to translate booleans?

Android Translate Animation Progress Callback

how to do pop up button animation in android studio

How to move (translate animation) a button with clickable area?

How to stop animation in android studio?

How to do a bounce animation on Android?

How to set transparent background during translate animation android?

android tween animation: how does coordinates works in translate?

How to add animation on a button in android

After translate animation button click not working

How to start Translate Animation in random order in Android?

How do I make shuffle playlist button and repeat button in android studio

How to apply colour animation to button in Android Studio like what shown by material design?

How to translate view with smooth animation in android

How to give animation to FloatingActionButton in android studio

How to retrigger function (translate animation )

Android studio - How to create curved lines with animation

How to make animation in Android Studio button?

Button animation in Android studio

how to translate the content of a button?

How to make the image rotate animation on button click in andorid studio?

How to do a "previous" question button in Android studio + firebase