开始逐帧动画 [英] Starting Frame-By-Frame Animation

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

问题描述

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

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

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

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

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

但是如果我将这一行放在按钮的 onClick() 回调方法中,按下按钮就会启动动画.

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

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

Why doesn't this line work in the 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(); 
}

}

推荐答案

需要注意的是 AnimationDrawable 上调用的 start() 方法不能在你的 Activity 的 onCreate() 方法中调用,因为 AnimationDrawable 还没有完全贴在窗户上.如果您想立即播放动画而不需要交互,那么您可能需要从 Activity 中的 onWindowFocusChanged() 方法调用它,当 Android 将您的窗口置于焦点时,该方法将被调用.页面的最后http://developer.android.com/guide/topics/graphics/2d-graphics.html

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

 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>  

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

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