我目前正在尝试在 ImageView 中播放动画。
此代码工作正常:
//Run Animations
ImageView char1 = (ImageView) findViewById(R.id.char1);
//set the animation drawable as background
char1.setBackgroundResource(R.anim.male03idle);
//create an animation drawable using the background
AnimationDrawable char1anim = (AnimationDrawable) char1.getBackground();
//start the animation
char1anim.start();
我想用一个字符串来指向 R.anim 中的这个动画。
例如:
String char1resource = "male" + "03" + "idle";
然后在 char1.setBackgroundResource 中使用 char1resource。
我试过这样的东西,但它不起作用:
String c1r = "male" + "03" + "idle";
int char1Resource = getResources().getIdentifier(c1r, null, getPackageName());
Drawable image = getResources().getDrawable(char1Resource);
//Run Animations
ImageView char1 = (ImageView) findViewById(R.id.char1);
//set the animation drawable as background
char1.setBackgroundResource(image);
//create an animation drawable using the background
AnimationDrawable char1anim = (AnimationDrawable) char1.getBackground();
//start the animation
char1anim.start();