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

时间:2012-02-06 07:08:37

标签: android drawable android-resources

我有这样的数组。

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};

现在我有6张图片,所以我静静地给出了这个名字。

如果我有大约50张图片,我不能给出数组中的每个文件名,所以它需要是动态的,我怎样才能实现这一点。

9 个答案:

答案 0 :(得分:110)

您可以使用getIdentifier()

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

答案 1 :(得分:12)

您也可以使用:

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

答案 2 :(得分:7)

这样的事情可以起作用

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

答案 3 :(得分:2)

使用以下行动态获取drawable:

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

这将为您提供所需的Drawable。

答案 4 :(得分:2)

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

答案 5 :(得分: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()));
    }
}

答案 6 :(得分:0)

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

如果你有gif的多个分割图像,下面的代码可用于显示像gif incase的图像。只需将gif从在线工具拆分为单独的png,然后将图像放入drawable中,如下面的顺序

image_1.png,image_2.png等

让处理程序动态更改图像。

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);
// 
                    }
                });
              }

答案 7 :(得分: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);
}
}

答案 8 :(得分:-10)

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

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

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