Android 从ImageView获取可绘制的图像

Android 从ImageView获取可绘制的图像,android,drawable,android-imageview,Android,Drawable,Android Imageview,我目前正在构建一个应用程序,主视图上有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++]

我目前正在构建一个应用程序,主视图上有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;
}
publicstaticvoidinitdecks(){
int m=0;
对于(int i=0;i
洗牌组:

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;
}
publicstaticstring[]getCards(int-size){
字符串[]结果=新字符串[大小];
对于(int i=0;i
在我的主要活动中,我将这些卡分配给视图,当用户单击它们时,我想知道图像是一张技巧卡还是一张普通卡


有没有办法找出被点击的卡片是否是一个诡计?类似于
if(image.getImageDrawable().equals(R.drawable.trick.jpg)

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

请查看此链接

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

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;
}
我发现
Drawable
资源id存储在
ImageView
对象的
mResource
字段中


请注意,
ImageView
中的
Drawable
可能并不总是来自资源id。它可能来自
Drawable
对象或图像Uri。在这些情况下,
mResource
将重置为0。因此,不要太依赖它。

请查看此链接。。。。。。。。。。。。。。。。。。