Android AnimationDrawable无法播放
因此,我希望我的动画在活动创建后立即开始,但出于某种原因,无论我尝试什么,都会让它开始。我可以让它开始有一个点击事件,但我希望它开始自己的一切 这是我所拥有的,我如何让它工作Android AnimationDrawable无法播放,android,animation,drawable,Android,Animation,Drawable,因此,我希望我的动画在活动创建后立即开始,但出于某种原因,无论我尝试什么,都会让它开始。我可以让它开始有一个点击事件,但我希望它开始自己的一切 这是我所拥有的,我如何让它工作 package tween.learn; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.widget.ImageView;
package tween.learn;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class Animate extends Activity {
public ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);
tweenImage.setBackgroundResource(R.anim.cubicfacetween);
AnimationDrawable frameAnimation =
(AnimationDrawable) tweenImage.getBackground();
frameAnimation.start();
}
}
谢谢尝试在窗口获得焦点后,通过覆盖活动中的onWindowFocusChanged来启动动画:
@Override
public void onWindowFocusChanged (boolean hasFocus)
{
//Start animation here
}
请参阅此处的文档:我认为您必须在相关视图的初始化完成后启动动画。您应该能够执行以下操作:
final ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);
tweenImage.setBackgroundResource(R.anim.cubicfacetween);
tweenImage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation =
(AnimationDrawable) tweenImage.getBackground();
frameAnimation.start();
}
}
Edit-这个问题让我相信windowfocuschanged方法并不总是有效的。它看起来确实更简单,如果对你有用的话,它可能是一个更好的主意。我的解决方案是在动画可见时播放动画。它允许您声明(在xml布局中)并忘记
class AnimatedImageView extends ImageView {
// required constructors omitted for clarity
private void updateAnimationsState() {
boolean running = getVisibility() == View.VISIBLE && hasWindowFocus();
updateAnimationState(getDrawable(), running);
updateAnimationState(getBackground(), running);
}
private void updateAnimationState(Drawable drawable, boolean running) {
if(drawable instanceof AnimationDrawable) {
AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
if(running) {
animationDrawable.start();
} else {
animationDrawable.stop();
}
}
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
updateAnimationsState();
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
updateAnimationsState();
}
}
如果从代码中设置drawable,那么应该重写相应的set*()方法,并从那里调用updateAnimationsState()。我知道这是一个老问题,但我也遇到了同样的问题,但答案对我没有帮助。在花了几个小时研究它之后,我终于发现我的问题是我已经将
android:src=“@drawable/myanimation”
添加到imageview容器中。一旦我去掉这个,上面的答案就起作用了。我认为动画正在运行,但通过设置src,动画的第一个图像位于其上方,因此我认为它没有播放
- 我的最终代码有一个XML文件,其中动画保存在drawable文件夹中
- 我的布局有一个没有定义android:src的imageview,并且设置为不可见
- 在
中,我将imageview设置为可见,并将onCreate
设置为XML文件中描述的动画setBackgroundResource
- 在
中,我启动动画李>onWindowFocusChanged
调用
animationDrawable.stop()
然后调用start()
方法。我通过调用setBackgroundResource
而不是setImageResource
解决了这个问题,当我想按照
注意:要重置动画,请使用
animationDrawable.selectDrawable(0);//animation drawable used here is collection of images
animationDrawable.stop();
这样做的诀窍,我想踢的框架动画文档的作者在螺母现在…WTH!这是完美的,但是“官方”文档不值得去做。谢谢Matthew Willis。我希望ImageView的缩放也能产生效果,所以我使用了src,将setBackground()和getBackground()替换为setImageResource()和getImageResource()。请您详细说明您的答案,并对您提供的解决方案进行详细说明。这也是我的问题。这么多的时间浪费在寻找答案上。我自己没抓到,因为我的另一个动画可以很好地使用src=。。。。在我的视野里。奇怪的非常感谢。
animationDrawable.selectDrawable(0);//animation drawable used here is collection of images
animationDrawable.stop();