我跟着this创建了一个简单的动画。
该示例在主布局中手动创建ImageView
,具有此类资源android:src="@anim/anim_android
问题是,当我创建动态ImageView
时,我将资源设置为此myImageView.setImageResource(R.anim.anim_android);
,动画不起作用,只查看序列的第一个图像。
任何帮助?
感谢。
答案 0 :(得分:6)
你应该从你imageView
开始制作动画:
myImageView.setImageResource(R.anim.anim_android);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myImageViewn.getDrawable();
myImageView.post(
new Runnable(){
@Override
public void run() {
myAnimationDrawable.start();
}
});
}
编辑:我认为您没有将ImageView
添加到您的contentView
的布局(Activity
);试试这个:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rootLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rootLayout.setLayoutParams(params);
ImageView myAnimation = new ImageView(this);
RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
myAnimation.setLayoutParams(paramsImage);
myAnimation.setImageDrawable(getResources().getDrawable(R.anim.anim_android));
rootLayout.addView(myAnimation);
setContentView(rootLayout);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(new Runnable() {
@Override
public void run() {
myAnimationDrawable.start();
}
});
}