我关注 http://developer.android.com/guide/topics/graphics/view-animation.html#frame-animation有微小的变化。我决定制作动画循环并希望它从一开始就开始。
我的动画位于 drawable/listening.xml:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/l_01"
android:duration="200" />
<item
android:drawable="@drawable/l_02"
android:duration="200" />
<item
android:drawable="@drawable/l_03"
android:duration="200" />
</animation-list>
还有我的初始化代码:
@Override public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
animImg = (ImageView)findViewById(R.id.listen_anim);
animImg.setBackgroundResource(R.drawable.listening);
anim = (AnimationDrawable) animImg.getBackground();
anim.start();
};
我只看到第一帧,没有其他图像。
最佳答案
教程里已经写好了:
需要注意的是,AnimationDrawable 上调用的 start() 方法不能在 Activity 的 onCreate() 方法期间调用,因为 AnimationDrawable 尚未完全附加到窗口。
如果你想立即播放动画,不需要交互,那么你可能想从你的 Activity 中的 onWindowFocusChanged() 方法中调用它,当 Android 带来你的窗口成为焦点。
因此,根据您的意愿,将您的电话从这两个地方之一开始。根据您的评论,将您的调用移至 onWindowsFocusChanged() 内开始。
编辑 所以这是“怎么做”:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
textView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
android.R.anim.slide_in_left|android.R.anim.fade_in));
}
}
需要注意的点有:
- 别忘了写 if/else case 来检查焦点
- 并删除自动生成的"super.onWindowFocusChanged(hasFocus);"
关于android - 如何在 onCreate 后立即启动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7289827/