我在drawable文件夹中有四张图片:small_blue.jpg,small_green.jpg,big_blue.jpg和big_green.jpg
我创建了一个带有两个参数的函数:
public Bitmap getPic (String size, String color)
{
return BitmapFactory.decodeResource( getResources(), R.drawable.small_blue);
}
我需要使用函数的参数更改R.drawable.small_blue中的small_blue 但我做不到:
R.drawable. + size + "_" + color
怎么做?
非常感谢
答案 0 :(得分:1)
试试这个:
public Bitmap getPic (String size, String color)
{
return
BitmapFactory.decodeResource
(
getResources(), getResourceID(size + "_" + color, "drawable", getApplicationContext())
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}