3

I am currently building an app that has 6 images on the main view and it generates random images from my drawable folder (shuffling a deck of cards)

initializing the decks:

public static void initDecks() {
    int m = 0;
    for (int i = 0; i < suites.length; i += 1) {
        for (int j = 0; j < regularCards.length; j += 1) {
            regularDeck[m++] = "drawablw/" + suites[i] + regularCards[j]
                    + ".jpg";
        }
    }
    m = 0;
    for (int i = 0; i < suites.length; i += 1) {
        for (int j = 0; j < trickCards.length; j += 1) {
            trickDeck[m++] = "drawable/" + suites[i] + trickCards[j]
                    + ".jpg";
        }
    }

    Collections.shuffle(Arrays.asList(regularDeck));
    Collections.shuffle(Arrays.asList(trickDeck));
}

shuffle the deck:

public static String[] getCards(int size) {
    String[] result = new String[size];
    for (int i = 0; i < size - 2; i += 1) {
        result[i] = regularDeck[i];
    }
    result[size - 1] = trickDeck[0];
    Collections.shuffle(Arrays.asList(result));
    return result;
}

in my main activity, I assign the cards to the view and when the user clicks on them, I want to know if the image is a trick card or a regular one.

is there a way to find out if the card that was clicked a trick one or not? something like if(image.getImageDrawable().equals(R.drawable.trick.jpg)?

thepoosh
  • 12,497
  • 15
  • 73
  • 132

3 Answers3

1

Store the name/id of the drawable at the point where you set it to the ImageView. So you always have a member variable in your class/activity that holds the current image identifier.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
0

Please look ovet this link

Get the ID of a drawable in ImageView

Basically you can set the id in ImageView tag when ever you set the image in ImageView and get from that where you need....

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0
public static Integer getDrawableId(ImageView obj)
    throws SecurityException, NoSuchFieldException,
    IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
        Field f = ImageView.class.getDeclaredField("mResource");
        f.setAccessible(true);
        Object out = f.get(obj);
        return (Integer)out;
}

I found that the Drawable resource id is stored in mResource field in the ImageView object.

Notice that Drawable in ImageView may not always come from a resource id. It may come from a Drawable object or image Uri. In these cases, the mResource will reset to 0. Therefore, do not rely on this much.

Yeung
  • 2,202
  • 2
  • 27
  • 50