Ben
Ben

Reputation: 3042

How to load multiple drawables from id using a loop?

Must be a way looping through this code:

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

Thanks!

Upvotes: 0

Views: 2290

Answers (5)

Eduardo Tolentino
Eduardo Tolentino

Reputation: 1699

This works for me

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

Upvotes: 0

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Please look at below code for that.

First make int array

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

or Set this Image to ImageView using below code

mImgView1.setImageResource(mImgArray[0]);

And you can convert this image directly to bitmap and store into bitmap array using below code.

Bitmap mBmpArray=new Bitmap[mImgArray.length];
for(int i=0; i<mImgArray.length; i++)
    mBmpArray[i] = BitmapFactory.decodeResource(getResources(), mImgArray[i]);
}

Upvotes: 0

user370305
user370305

Reputation: 109237

If I am understand your question, you want something like,

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

Upvotes: 2

Cata
Cata

Reputation: 11211

You can do that in a simple for loop.. and start from the first id and increment it by 1 at each loop.. though I don't recommend you to do this..

Upvotes: -1

Moritz
Moritz

Reputation: 10352

You should place your images in the assets folder. From their you can access them via their file name. See http://developer.android.com/reference/android/content/res/AssetManager.html.

Upvotes: 1

Related Questions