开始和停止动画 [英] Starting and stopping animation

查看:122
本文介绍了开始和停止动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚我该如何在Android App中启动和停止动画。

I am trying to figure out how on earth i can start and stop animation in my Android App.

@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();
    }

}

这几乎是我拥有的一切,即时消息是从零开始的,没有将任何按钮绑定到功能上,但是图像根本没有旋转。

This is pretty much everything i have, and im starting from scratch without binding any buttons to the functionality, but the image is not spinning around at all.

这是我的可绘制XML文件:

This is my XML file for the drawable:

<?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>

按钮的更多代码:

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.buttonStopOne:

        break;

        case R.id.buttonStopTwo:
        break;

        case R.id.buttonStopThree:
        break;

        case R.id.buttonStart:
            frameAnimation.start();
        break;
    }
} 

有人能确定我在做什么错吗?

Can anyone determine what im doing wrong?

推荐答案

您必须将动画应用于视图。因此,假设 firstImage()是要制作动画的动画,请调用 firstImage.startAnimation(frameAnimation)。然后结束它,调用 firstImage.clearAnimation()

You have to apply the animation to a View. So assuming firstImage() is the animation you want to animate, call firstImage.startAnimation(frameAnimation). Then to end it call firstImage.clearAnimation().

编辑:

好。现在,我知道您在做什么,我猜您正在使用的 Runnable 永远不会被调用。您可以尝试的是等到视图完全膨胀后再开始动画。像这样的东西:

Ok. Now that I see what you're doing, I'm guessing the Runnable you're using is never being called. What you can try is wait until the view has been fully inflated and start the animation in that. Something like this:

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();
   }
}

现在,我还没有尝试过,但是我想的是

Now, I haven't tried this, but what I'm thinking will happen is when the view is fully inflated and visible, the animation will start.

旁注:这也是确定视图何时具有尺寸的便捷方法。

这篇关于开始和停止动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆