Set duration for translate animation in xml

1.4k Views Asked by At

I just started learning animations and I was wondering how can I set the duration of an animation (scale, translate, rotate etc..) in the XML file?

This is my translate.xml resource file:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-1000"
    android:toXDelta="0">
</translate>

I watched some tutorials on Youtube and noticed that they used android:duration="2000" (this means 2 seconds if I'm not mistaken?), something like this:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-1000"
    android:toXDelta="0"
    android:duration="2000">
</translate>

However, I was unable to add that line of code inside my XML file. I am doing something wrong here?

UPDATE: After multiple times of restarting my Android Studio project, I was able to add that line of code. (Previously the IDE was displaying red squiggly lines)

2

There are 2 best solutions below

0
snachmsm On BEST ANSWER

everything looks fine, duration should work as TranslateAnimation extends Animation, which handle duration attribute

0
Parth Pitrubhakta On

In the your xml file the root element should be set inside set you should define duration. Then you can override that duration in translate.

Here is the code

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

        <translate
            android:duration = "500"
            android:fromYDelta="100%"
            android:toYDelta="0%" />

</set>