How to get all resables res IDs from animation list?

If I have an animated XML graphic file (animation list) that I can reference with R.drawable.anim, is there a way to get all the identifiers Drawablein the array?

Basically, I don’t want to somehow load the xml file and get a list of everyone Drawableinvolved in this animation.

Here is the xml animation I'm using

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/typer_step_1"
        android:duration="50"/>
    <item
        android:drawable="@drawable/typer_step_2"
        android:duration="50"/>
    <item
        android:drawable="@drawable/typer_step_3"
        android:duration="50"/>
    <item
        android:drawable="@drawable/typer_step_4"
        android:duration="50"/>

</animation-list>
+4
source share
2 answers

<string-array name="random_imgs">
    <item>@drawable/image1</item>
    <item>@drawable/image2</item>
    <item>@drawable/image3</item>
</string-array>

in your activity

   TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
   //get resourceid by index
   imgs.getResourceId(i, -1);
   // or set you ImageView resource to the id
   mImgView1.setImageResource(imgs.getResourceId(i, -1));
   imgs.recycle();
+2
source

you can use reflection.

import field class

import java.lang.reflect.Field;

the code

Field[] Fields = R.drawable.class.getFields();
int[] resArray = new int[Fields.length];
for(int i = 0; i < Fields.length; i++) {
    try {
        resArray[i] = Fields[i].getInt(null);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

arrays.xml res, :

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="random_imgs">
        <item>@drawable/car_01</item>
        <item>@drawable/balloon_random_02</item>
        <item>@drawable/dog_03</item>
    </string-array>

</resources>

:

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
//get resourceid by index
imgs.getResourceId(i, -1)
// or set you ImageView resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));
0

All Articles