我正在尝试逐帧显示动画,并希望遍历可绘制对象,这样我就不必输入它们的所有名称,以防帧数增加。
但是我似乎无法找到如何遍历可绘制对象。我查阅了一些 java for 循环教程,但它们都只是打印了一些(据我确定)不必在此处使用的内容。
相关代码如下(图片名称为dude1, dude2, ...):
private void startAnimation(){
animation = new AnimationDrawable();
for (int i = 1; i < 4; i++) {
animation.addFrame(getResources().getDrawable(R.drawable.dude(i)), 100);
}
animation.setOneShot(true);
ImageView imageView = (ImageView) findViewById(R.id.img);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 90);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setImageDrawable(animation);
imageView.post(new Starter());
}
谢谢!
最佳答案
我认为最后一个答案不太正确,但可能是:
for (int i=0; i <10;i++){
animation.addFrame(
getResources().getDrawable(getResources().getIdentifier("dude" + i,"drawable",
getPackageName()),100);
}
关于Android 如何迭代一个 R.drawable 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9948105/