Java 启动和停止动画
我正试图弄清楚我到底如何在Android应用程序中启动和停止动画Java 启动和停止动画,java,android,animation,Java,Android,Animation,我正试图弄清楚我到底如何在Android应用程序中启动和停止动画 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); stopOneButton = (Button) findViewById(R.id.buttonStopOne); stopTwoButton
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stopOneButton = (Button) findViewById(R.id.buttonStopOne);
stopTwoButton = (Button) findViewById(R.id.buttonStopTwo);
stopThreeButton = (Button) findViewById(R.id.buttonStopThree);
startButton = (Button) findViewById(R.id.buttonStart);
firstImage = (ImageView) findViewById(R.id.imageView1);
firstImage.setBackgroundResource(R.drawable.spin_animation);
frameAnimation = new AnimationDrawable();
AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();
// frameAnimation.start();
firstImage.post(new Starter());
stopOneButton.setOnClickListener(this);
stopTwoButton.setOnClickListener(this);
stopThreeButton.setOnClickListener(this);
startButton.setOnClickListener(this);
}
class Starter implements Runnable {
public void run() {
frameAnimation.start();
}
}
这几乎是我所拥有的一切,我从零开始,没有将任何按钮绑定到功能,但图像根本没有旋转
这是我的可绘图的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/image1" android:duration="500" />
<item android:drawable="@drawable/image2" android:duration="500" />
<item android:drawable="@drawable/image3" android:duration="500" />
<item android:drawable="@drawable/image4" android:duration="500" />
</animation-list>
有人能确定我做错了什么吗?您必须将动画应用于视图。因此,假设
firstImage()
是要设置动画的动画,请调用firstImage.startAnimation(frameAnimation)
。然后调用firstImage.clearAnimation()
,结束它
编辑:
嗯。现在我知道你在做什么了,我猜你正在使用的Runnable
永远不会被调用。您可以尝试等待视图完全充气,然后在该视图中启动动画。大概是这样的:
firstImage = (ImageView) findViewById(R.id.imageView1);
final AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();
ViewTreeObserver treeObserver = firstImage.getViewTreeObsver();
treeObvserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout(){
frameAnimation.start();
}
}
现在,我还没有尝试过这个,但是我认为当视图完全膨胀并且可见时,动画就会开始
旁注:我发现这有点让人困惑,
frameAnimation.start()代码>不启动我的动画?另一种启动动画的方法是在视图上调用setAnimation
,传入动画,然后在动画本身上调用start()
。因此,在您的例子中,它将是firstimage.setAnimation(frameAnimation)代码>集中的某个地方。然后,当您希望播放名为frameAnimation.start()
的动画时。我几乎可以肯定,它没有为您设置动画的原因是动画没有引用任何视图。哦,等等。我想我明白你在做什么了。您正在使用动画列表创建一个可绘制图形,并将其设置为背景。我很抱歉。这个答案没有帮助。我的另一个猜测是你的runnable从未被调用过。我将编辑我的答案,因为我想我有一个想法。使用post只会在视图的消息队列中添加一个runnable。它不会自动被调用。我的代码中的编辑应该确保它总是被自动调用。如果没有,请告诉我,因为我还没有试过。谢谢,迪夫。但是,如果我们假设我想通过按下按钮来实现启动和停止呢?那就不应该再是问题了?
firstImage = (ImageView) findViewById(R.id.imageView1);
final AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();
ViewTreeObserver treeObserver = firstImage.getViewTreeObsver();
treeObvserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout(){
frameAnimation.start();
}
}