سؤال

I am new to animations in android so please don't mark me negative. I am trying to animate positon of a button using translate animation.

What I want to do:

When user click the button, the button should first move towards extreme right of the display such that it is no longer visible. It should then come back to its original position from the left of the display.

What I want

What actually is happening (Problem):

When user clicks the button, the button first comes from the left of display and then accelerate towards the right of the display.

animation.xml :

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

    <translate
        android:duration="200"
        android:fromXDelta="0%p"
        android:fillAfter="true"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="100%p" />
    <translate
        android:fromXDelta="-100%p"
        android:duration="200"
        android:fillAfter="true"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:startOffset="200"
        android:toXDelta="0%p" />

</set>
هل كانت مفيدة؟

المحلول

Dude try this code it think it will fix your issue:

<translate
    android:duration="200"
    android:fromXDelta="0%p"
    android:fillAfter="true"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toXDelta="100%p" />
<translate

    android:fromXDelta="100%p"
    android:duration="1"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:startOffset="200"
    android:toXDelta="-100%p" />
<translate

    android:fromXDelta="-100%p"
    android:duration="200"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:startOffset="210"
    android:toXDelta="0%p" />

Just remember the first translation will take your button to the right , so that mean the current button position is situated there (100%p) base on your animation set, so you need to find a way to set it quickly to the position you want it to start next, hence the translate i added in the middle.

Hope it will help

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top