50

我有一个这样的数组。

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 张图像,我无法在数组中给出每个文件名,所以它需要是动态的,我该如何实现这一点。

4

9 回答 9

115

您可以使用getIdentifier()

for (int j = 1; j < 6; j++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}
于 2012-02-06T07:32:38.550 回答
13

你也可以使用这个:

int res = getResources().getIdentifier("<your pakecgename>:drawable/abc", null, null);
于 2012-02-06T08:02:09.413 回答
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();
    }
}
于 2012-02-06T07:23:01.420 回答
4

使用以下行动态获取drawable:

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

这将为您提供所需的 Drawable。

于 2012-02-06T07:21:06.773 回答
2
public static Drawable getImage(Context context, String name) {
        return context.getResources().getDrawable(context.getResources().getIdentifier(name, "drawable", context.getPackageName()));
}
于 2013-12-24T10:47:08.493 回答
2
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()));
    }
}
于 2015-09-22T11:05:22.773 回答
1
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);
}
}
于 2018-03-09T01:43:08.283 回答
0

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

如果您有 gif 的多个拆分图像,则以下代码可用于显示类似 gif 的图像。只需从在线工具将 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);
// 
                    }
                });
              }
于 2018-02-01T07:25:13.097 回答
-10

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

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

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

于 2012-02-06T07:16:38.630 回答