简体   繁体   中英

What is the best way to deal with adding Drawable images programmatically?

I'm working on an app that deals with playing cards. Right now, I have a folder, drawable/cards that has dozens of files, numbered sequentially from 00.png , that are images of the card faces.

I also have two classes, Deck and Card . Card s have a reference to the Drawable image of their face. Deck has a list of Card s that I would like to construct within a for loop. This all looks like,

public class Card{

    public Card(Drawable image) {}

};
public class Deck{

    List<Card> cardList;

    public Deck(){
        for(int i=0; i<52; ++i){
            cardList.add( new Card( ??? ) );
        }
    }

}

My question is, is there any way for me to generate references to the Drawable images of the card faces when constructing the Card s this way? Or do I need to find a different approach?

Have you already tried handing over the resource id of your drawable image instead of the whole drawable itself? Then you should be able to use this to draw the image in your Card-class using the setImageResource() function on your imageView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM