androidandroid-resources

How do I get a particular value in an animation resource file?


I want to get the duration value in my animation resource file at runtime.

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

    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:duration = "80"
        android:fromXScale = "1"
        android:fromYScale = "1"
        android:pivotX = "50%"
        android:pivotY = "50%"
        android:toXScale = "0.96"
        android:toYScale = "0.96"/>
</set>

File is R.anim.zoom.

I tried..

getResources().getAnimation(R.anim.zoom)
  .getAttributeIntValue("android", "duration", 50)

and

getResources().getAnimation(R.anim.zoom)
  .getAttributeIntValue("http://schemas.android.com/apk/res/android", "duration", 50)

and

getResources().getAnimation(R.anim.zoom)
  .getAttributeIntValue("http://schemas.android.com/apk/res/android", "android:duration", 50)

and

String duration = getResources().getAnimation(R.anim.zoom_out)
  .getAttributeValue("http://schemas.android.com/apk/res/android", "android:duration");

and

String duration = getResources().getAnimation(R.anim.zoom_out)
  .getAttributeValue("http://schemas.android.com/apk/res/android", "duration");

but it always returns default value or null (resource not found).


Solution

  • Please try

    Animation animation = AnimationUtils.loadAnimation(context, R.anim.zoom);
    long duration = animation.getDuration();