我有一个关于开始逐帧动画的基本问题。
当我从代码中直接调用AnimationDrawable.start()方法时,它似乎不起作用。
public void onCreate(Bundle savedInstanceState) {
...
mAnimation.start();
...
}
但是,如果我将这一行放在按钮的onClick()回调方法中,按下按钮按钮将启动动画。
为什么这一行在代码中不起作用?
谢谢!
代码:
public class MyAnimation extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
AnimationDrawable mframeAnimation = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.my_animation);
ImageView img = (ImageView) findViewById(R.id.imgMain);
BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable(
R.drawable.splash1);
BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable(
R.drawable.splash2);
int reasonableDuration = 250;
mframeAnimation = new AnimationDrawable();
mframeAnimation.setOneShot(false);
mframeAnimation.addFrame(frame1, reasonableDuration);
mframeAnimation.addFrame(frame2, reasonableDuration);
img.setBackgroundDrawable(mframeAnimation);
mframeAnimation.setVisible(true, true);
//If this line is inside onClick(...) method of a button, animation works!!
mframeAnimation.start();
}
}
发布于 2010-05-08 02:47:59
需要注意的是,在活动的onCreate()方法期间不能调用在AnimationDrawable上调用的start()方法,因为AnimationDrawable还没有完全附加到窗口。如果你想立即播放动画,而不需要交互,那么你可能想从你的活动中的onWindowFocusChanged()方法调用它,当Android将你的窗口置于焦点时,它就会被调用。页面末尾的http://developer.android.com/guide/topics/graphics/2d-graphics.html
ImageView img = (ImageView)findViewById(R.id.some layout);
AnimationDrawable frameAnimation = (AnimationDrawable)img.getDrawable();
frameAnimation.setCallback(img);
frameAnimation.setVisible(true, true);
frameAnimation.start();
要添加动画,您可以执行以下操作
<animation-list android:id="@+id/my_animation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/frame1" android:duration="150" />
<item android:drawable="@drawable/frame2" android:duration="150" />
</animation-list>
发布于 2010-09-22 22:34:34
使用Runnable将start()消息插入到消息队列中,只需添加此LOC来替换您的mFrameAnimation.start();
img.post(new Starter());
帮助器内部类:
class Starter implements Runnable {
public void run() {
mFrameAnimation.start();
}
}
发布于 2012-03-18 09:11:17
仅在onCreate中播放动画(...)添加:
ImageView mImageView=(ImageView) findViewById(R.id.image);
mImageView.setBackgroundResource(R.anim.film);
mFrameAnimation = (AnimationDrawable) mImageView.getBackground();
mImageView.post(new Runnable(){
public void run(){
mFrameAnimation.start();
}
});
https://stackoverflow.com/questions/2785336
复制相似问题