如何使用循环从id加载多个drawable?

时间:2012-06-22 08:52:46

标签: android resources

必须循环使用此代码:

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

谢谢!

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

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

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

答案 4 :(得分:-1)

你可以在一个简单的for循环中做到这一点..从第一个id开始并在每个循环中将它递增1 ..虽然我不建议你这样做..