我想知道如何从作为图像名称/类型字符串的动态变量设置位图可绘制资源:
public CustomIcon getView(int position, View convertView, ViewGroup parent) {
String label;
String image;
DataBaseManager db = new DataBaseManager(context);
label = db.getIconLabel(mIcons.get(position));
image = db.getIconImage(mIcons.get(position));
Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(),
parent.getResources().getIdentifier(image, "drawable", getApplicationContext().getPackageName()));
Log.v(TAG, "current pos:"+ position);
CustomIcon icon = new CustomIcon(context, bitmap,label);
return icon;
}
有问题的部分是
Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(),
R.drawable.[image]);
上面的代码已更改 -
最佳答案
处理方法是先获取资源ID,再使用BitmapFactory方法:
String imgName = "myicon";
int resID = parent.getResources().getIdentifier(imgName, "drawable", "my.app.package.name");
Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(), resID);
关于android - 在android中可绘制的动态位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9621540/