如何从drawable文件夹动态获取图像?

时间:2021-11-23 03:14:01

I have an array like this.

我有一个这样的数组。

int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003,
                   R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006};

Right now I have 6 images so I am statically given the name.

现在我有6个图像,所以我静态地给它命名。

If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this.

如果我有50个图像,我不能给数组中的每个文件名,所以它必须是动态的,我怎么才能实现这个。

9 个解决方案

#1


106  

You can use getIdentifier()

您可以使用getIdentifier()

for (int j = 1; j < 6; j++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}

#2


12  

You can also use this:

你也可以用这个:

int res = getResources().getIdentifier("<your pakecgename>:drawable/abc", null, null);

#3


6  

Something like this could work

像这样的东西可以用

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

#4


2  

public static Drawable getImage(Context context, String name) {
        return context.getResources().getDrawable(context.getResources().getIdentifier(name, "drawable", context.getPackageName()));
}

#5


2  

Use the following line for getting drawable dynamically:

使用以下行动态绘制:

Drawable drawable = this.getResources().getDrawable(R.drawable.yourDrawableID);

This will give you the desired Drawable.

这会给你想要的画。

#6


1  

String[] names = new String []{"yout names..."};
    for(String n: names) {
        Utils.GetDrawableByName(n,this);
    }

public class Utils {
public static Drawable GetDrawableByName(String name,Activity context){
    Resources res = context.getResources();
    return res.getDrawable(res.getIdentifier(name,"drawable",context.getPackageName()));
    }
}

#7


0  

We can take advantage of Imageview setImageResource as this will efficient than drawable seems, refer below code for the same.

我们可以利用Imageview setImageResource,因为这比drawable看起来更有效,请参考下面的代码。

The below code can be used to show the image like gif incase if you have the multiple split image of gif. Just split the gif into individual png from a online tool and put image in the drawable like the below order

下面的代码可以用来显示gif这样的图像,如果你有gif的多个分割图像。只需将gif从一个在线工具中拆分为单独的png,并按如下顺序将图像放入可绘制的文件中

image_1.png, image_2.png, etc.

image_1。png、image_2。png等。

Have the handler to change the image dynamically.

让处理程序动态地改变图像。

int imagePosition = 1;
    Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                updateImage();
            }
        };




    public void updateImage() {

                appInstance.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
                        gifImageViewDummy.setImageResource(resId);
                        imagePosition++;
    //Consider you have 30 image for the anim
                        if (imagePosition == 30) {
//this make animation play only once
                            handler.removeCallbacks(runnable);

                        } else {
    //You can define your own time based on the animation
                            handler.postDelayed(runnable, 50);
                        }

//to make animation to continue use below code and remove above if else
// if (imagePosition == 30)
//imagePosition = 1;
// handler.postDelayed(runnable, 50);
// 
                    }
                });
              }

#8


0  

package com.example.studio.snakes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

int[] dices = {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five,
        R.drawable.six,
};


public void rollTapped(View view){
 Log.i("Button","Button Tapped");
    Random rand = new Random();
    int randomnum = rand.nextInt(6);
    Log.i("Random","Random number is " + randomnum );
    ImageView dice=findViewById(R.id.imageView2);
    dice.setImageResource(dices[randomnum]);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

#9


-10  

use this code to create array and later use that array

使用此代码创建数组,然后使用该数组

int image[] = new int[50];
for (int i = 1 ; i <= 50 ; i++)
{
images[i]  = "R.drawable.d002_p00"+i;
}

main thing you have to take care is the file name must start with "d002_p00" this and after there is digit 1 to 50

你要注意的主要事情是文件名必须以“d002_p00”开头,在数字1到50之后

#1


106  

You can use getIdentifier()

您可以使用getIdentifier()

for (int j = 1; j < 6; j++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}

#2


12  

You can also use this:

你也可以用这个:

int res = getResources().getIdentifier("<your pakecgename>:drawable/abc", null, null);

#3


6  

Something like this could work

像这样的东西可以用

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

#4


2  

public static Drawable getImage(Context context, String name) {
        return context.getResources().getDrawable(context.getResources().getIdentifier(name, "drawable", context.getPackageName()));
}

#5


2  

Use the following line for getting drawable dynamically:

使用以下行动态绘制:

Drawable drawable = this.getResources().getDrawable(R.drawable.yourDrawableID);

This will give you the desired Drawable.

这会给你想要的画。

#6


1  

String[] names = new String []{"yout names..."};
    for(String n: names) {
        Utils.GetDrawableByName(n,this);
    }

public class Utils {
public static Drawable GetDrawableByName(String name,Activity context){
    Resources res = context.getResources();
    return res.getDrawable(res.getIdentifier(name,"drawable",context.getPackageName()));
    }
}

#7


0  

We can take advantage of Imageview setImageResource as this will efficient than drawable seems, refer below code for the same.

我们可以利用Imageview setImageResource,因为这比drawable看起来更有效,请参考下面的代码。

The below code can be used to show the image like gif incase if you have the multiple split image of gif. Just split the gif into individual png from a online tool and put image in the drawable like the below order

下面的代码可以用来显示gif这样的图像,如果你有gif的多个分割图像。只需将gif从一个在线工具中拆分为单独的png,并按如下顺序将图像放入可绘制的文件中

image_1.png, image_2.png, etc.

image_1。png、image_2。png等。

Have the handler to change the image dynamically.

让处理程序动态地改变图像。

int imagePosition = 1;
    Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                updateImage();
            }
        };




    public void updateImage() {

                appInstance.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
                        gifImageViewDummy.setImageResource(resId);
                        imagePosition++;
    //Consider you have 30 image for the anim
                        if (imagePosition == 30) {
//this make animation play only once
                            handler.removeCallbacks(runnable);

                        } else {
    //You can define your own time based on the animation
                            handler.postDelayed(runnable, 50);
                        }

//to make animation to continue use below code and remove above if else
// if (imagePosition == 30)
//imagePosition = 1;
// handler.postDelayed(runnable, 50);
// 
                    }
                });
              }

#8


0  

package com.example.studio.snakes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

int[] dices = {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five,
        R.drawable.six,
};


public void rollTapped(View view){
 Log.i("Button","Button Tapped");
    Random rand = new Random();
    int randomnum = rand.nextInt(6);
    Log.i("Random","Random number is " + randomnum );
    ImageView dice=findViewById(R.id.imageView2);
    dice.setImageResource(dices[randomnum]);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

#9


-10  

use this code to create array and later use that array

使用此代码创建数组,然后使用该数组

int image[] = new int[50];
for (int i = 1 ; i <= 50 ; i++)
{
images[i]  = "R.drawable.d002_p00"+i;
}

main thing you have to take care is the file name must start with "d002_p00" this and after there is digit 1 to 50

你要注意的主要事情是文件名必须以“d002_p00”开头,在数字1到50之后