3

我目前正在构建一个在主视图上有 6 张图像的应用程序,它会从我的drawable文件夹中生成随机图像(洗牌)

初始化卡组:

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

洗牌:

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

在我的主要活动中,我将卡片分配给视图,当用户点击它们时,我想知道图像是一张特技卡片还是一张普通卡片。

有没有办法找出被点击的卡片是否是一个技巧?像if(image.getImageDrawable().equals(R.drawable.trick.jpg)什么?

4

3 回答 3

1

将可绘制对象的名称/ID 存储在您将其设置为 ImageView 的位置。因此,您的类/活动中始终有一个成员变量来保存当前图像标识符。

于 2012-05-24T10:16:46.357 回答
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.

于 2012-12-27T04:26:35.120 回答
0

请看一下这个链接

获取 ImageView 中可绘制对象的 ID

基本上,当您在 ImageView 中设置图像并从您需要的位置获取时,您可以在 ImageView 标记中设置 id。

于 2012-05-24T10:45:52.783 回答