Setting an image from drawable equal to a variable at runtime
I have two images in my drawable folder, one called "Red" and the other called "Yellow". I am trying to set these images equal to two variables, one currently called "redchip" and the other called "yellowchip". Both redchip and yellowchip are declared as integers.
Can I set the R.drawable image integer values e开发者_运维技巧qual to my variables, and if so, how?
What I have done in the past is use a map in a util class to return the drawable's integer id.
Example:
public class DrawableUtil{
public static final int KEY_CHIP_YELLOW = 0;
public static final int KEY_CHIP_RED = 1;
private Map<Integer, Integer> drawableMap = new HashMap<Integer, Integer>();
DrawableUtil(){
drawableMap.put(KEY_CHIP_YELLOW, R.drawable.yellowchip);
drawableMap.put(KEY_CHIP_RED, R.drawable.redchip);
}
public int getYellowChip(){
return drawableMap.get(KEY_CHIP_YELLOW);
}
}
精彩评论