根据图片名获取ID

public int getResourceId(String name){
try {
//根据资源的ID的变量名得到Field的对象,使用反射机制来实现的
Field field = R.drawable.class.getField(name);
//取得并返回资源的id的字段(静态变量)的值,使用反射机制
return Integer.parseInt(field.get(null).toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
web