How to load multiple drawables from id using a loo

2019-06-14 09:39发布

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!

4条回答
【Aperson】
2楼-- · 2019-06-14 09:49

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]);
}
查看更多
何必那么认真
3楼-- · 2019-06-14 10:00

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]);
}
查看更多
戒情不戒烟
4楼-- · 2019-06-14 10:09

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..

查看更多
戒情不戒烟
5楼-- · 2019-06-14 10:10

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.

查看更多
登录 后发表回答