0

必须是循环通过此代码的一种方式:

private void loadSprites() {
    this.sprites[0] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom01);
    this.sprites[1] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom02);
    this.sprites[2] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom03);
    this.sprites[3] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom04);
    this.sprites[4] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom05);
    this.sprites[5] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom06);
    this.sprites[6] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom07);
}

谢谢!

4

5 回答 5

2

如果我理解你的问题,你想要类似的东西,

int[] drawables = {R.drawable.ic_boom01,R.drawable.ic_boom02,R.drawable.ic_boom03,R.drawable.ic_boom04,R.drawable.ic_boom05,R.drawable.ic_boom06,R.drawable.ic_boom07}


private void loadSprites() {
for(int i=0; i<this.sprites.length; i++)
    this.sprites[i] = BitmapFactory.decodeResource(getResources(), drawables[i]);
}
于 2012-06-22T08:57:25.193 回答
1

您应该将图像放在资产文件夹中。从他们那里,您可以通过他们的文件名访问它们。请参阅http://developer.android.com/reference/android/content/res/AssetManager.html

于 2012-06-22T08:54:41.907 回答
0

请查看下面的代码。

首先制作int数组

int[] mImgArray = { R.drawable.ic_boom01, R.drawable.ic_boom02,
            R.drawable.ic_boom03, R.drawable.ic_boom04, R.drawable.ic_boom05,
            R.drawable.ic_boom06, R.drawable.ic_boom07 };

或使用以下代码将此图像设置为 ImageView

mImgView1.setImageResource(mImgArray[0]);

您可以使用以下代码将此图像直接转换为位图并存储到位图数组中。

Bitmap mBmpArray=new Bitmap[mImgArray.length];
for(int i=0; i<mImgArray.length; i++)
    mBmpArray[i] = BitmapFactory.decodeResource(getResources(), mImgArray[i]);
}
于 2012-06-22T09:04:09.523 回答
0

这对我有用

//Bitmap of images for fly asset
     private Bitmap fly[] = new Bitmap[8];
    
    //Initialize array of images
    for(int i = 0; i < fly.length; i++){
                this.fly[i] = BitmapFactory.decodeResource( getResources(), getResources().getIdentifier("fly_frame" + i, "drawable", context.getPackageName() ) );
    }
于 2021-07-06T02:20:33.910 回答
-1

您可以在一个简单的 for 循环中执行此操作..并从第一个 id 开始并在每个循环中将其递增 1.. 虽然我不建议您这样做..

于 2012-06-22T08:55:43.807 回答