Android 开始逐帧动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2785336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:21:37  来源:igfitidea点击:

Starting Frame-By-Frame Animation

androidanimation

提问by OceanBlue

I have a basic question about starting a frame-by-frame animation.

我有一个关于开始逐帧动画的基本问题。

When I call the AnimationDrawable.start()method from my code directly, it doesn't seem to work.

当我直接从我的代码中调用AnimationDrawable.start()方法时,它似乎不起作用。

public void onCreate(Bundle savedInstanceState) {  
   ...  
   mAnimation.start();  
   ...  
}

But if I put this line inside the onClick() callback method of a button, pressing the buton starts the animation.

但是,如果我将此行放在按钮的 onClick() 回调方法中,则按下按钮会启动动画。

Why doesn't this line work in the code?

为什么这一行在代码中不起作用?

Thanks!

谢谢!

Code:

代码:

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

}

}

回答by Alex Volovoy

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus. Very end of the page http://developer.android.com/guide/topics/graphics/2d-graphics.html

需要注意的是,在 Activity 的 onCreate() 方法期间不能调用 AnimationDrawable 上调用的 start() 方法,因为 AnimationDrawable 尚未完全附加到窗口。如果您想立即播放动画而不需要交互,那么您可能需要从 Activity 中的 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();

and to add animation you can do something like

并添加动画你可以做类似的事情

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

回答by will

Use a Runnable to insert the start() message to message queue, just add this LOC to replace your mFrameAnimation.start();

使用 Runnable 将 start() 消息插入消息队列,只需添加此 LOC 即可替换您的 mFrameAnimation.start();

img.post(new Starter());

Helper inner class:

辅助内部类:

class Starter implements Runnable {
        public void run() {
            mFrameAnimation.start();
        }
}

回答by javanayomnik

to play an animation just in onCreate(...) add:

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