[英]get drawable from 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++] = "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)
?
將drawable的名稱/ id存儲在將其設置為ImageView的位置。 因此,您的類/活動中始終有一個包含當前圖像標識符的成員變量。
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.因此,不要依賴這么多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.