Android 中的帧动画?

发布于 2024-09-27 05:24:40 字数 2107 浏览 4 评论 0原文

我正在使用帧动画来显示一些图像。但它仅在按钮操作中起作用。我想在程序启动时调用这个函数。没有按钮我怎样才能实现这一点?

我对动画使用以下代码:

public class FrameAnimationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setupButton();
    }
    private void setupButton(){
        Button b = (Button)this.findViewById(R.id.startFAButtonId);
        b.setOnClickListener(
          new Button.OnClickListener(){
            public void onClick(View v){
                parentButtonClicked(v);
            }
          });
    }
    private void parentButtonClicked(View v){
        animate();
    }
    private void animate(){
        ImageView imgView = (ImageView)findViewById(R.id.imageView);
        imgView.setVisibility(ImageView.VISIBLE);
        imgView.setBackgroundResource(R.drawable.frame_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
        if (frameAnimation.isRunning()){
            frameAnimation.stop();
        }
        else{
            frameAnimation.stop();
            frameAnimation.start();
        }
    }
}
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/colored-ball1" android:duration="50" />
    <item android:drawable="@drawable/colored-ball2" android:duration="50" />
    <item android:drawable="@drawable/colored-ball3" android:duration="50" />
    <item android:drawable="@drawable/colored-ball4" android:duration="50" />
    <item android:drawable="@drawable/colored-ball5" android:duration="50" />
    <item android:drawable="@drawable/colored-ball6" android:duration="50" />
    <item android:drawable="@drawable/colored-ball7" android:duration="50" />
    <item android:drawable="@drawable/colored-ball8" android:duration="50" />
</animation-list>

I am using frame animation for displaying some images. But it's working only in a button action. I want to call this function when the program starts. How can I achieve this with out a button?

I use the following code for the animation:

public class FrameAnimationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setupButton();
    }
    private void setupButton(){
        Button b = (Button)this.findViewById(R.id.startFAButtonId);
        b.setOnClickListener(
          new Button.OnClickListener(){
            public void onClick(View v){
                parentButtonClicked(v);
            }
          });
    }
    private void parentButtonClicked(View v){
        animate();
    }
    private void animate(){
        ImageView imgView = (ImageView)findViewById(R.id.imageView);
        imgView.setVisibility(ImageView.VISIBLE);
        imgView.setBackgroundResource(R.drawable.frame_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
        if (frameAnimation.isRunning()){
            frameAnimation.stop();
        }
        else{
            frameAnimation.stop();
            frameAnimation.start();
        }
    }
}
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/colored-ball1" android:duration="50" />
    <item android:drawable="@drawable/colored-ball2" android:duration="50" />
    <item android:drawable="@drawable/colored-ball3" android:duration="50" />
    <item android:drawable="@drawable/colored-ball4" android:duration="50" />
    <item android:drawable="@drawable/colored-ball5" android:duration="50" />
    <item android:drawable="@drawable/colored-ball6" android:duration="50" />
    <item android:drawable="@drawable/colored-ball7" android:duration="50" />
    <item android:drawable="@drawable/colored-ball8" android:duration="50" />
</animation-list>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-10-04 05:24:40

在 Android 的文档中,您可以找到以下内容:

“重要的是要注意,在 Activity 的 onCreate() 方法期间无法调用在 AnimationDrawable 上调用的 start() 方法,因为 AnimationDrawable 尚未完全附加到窗口如果您想立即播放动画,而不需要交互,那么您可能需要从 Activity 中的 onWindowFocusChanged() 方法调用它,当 Android 将您的窗口置于焦点中时,该方法将被调用。”

希望有帮助!

On the Android's Documentation, you can found the following:

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

Hope that helps!

戒ㄋ 2024-10-04 05:24:40

当您单击按钮时,唯一发生的事情就是调用 animate() 方法。您是否尝试过将其放在

animate();

onCreate() 内部并将其从 parentButtonClicked(View v) 中删除,以便在创建活动时而不是按下按钮时调用 animate ?不明白为什么这对你不起作用。

然后你还会有一个无用的按钮。 :)

When you click on your button, the only thing that happens is the animate() method is called. Have you tried placing

animate();

inside of onCreate() and removing it from parentButtonClicked(View v) so that it calls animate when the activity is created, rather than when the button is pressed? Don't see why that wouldn't work for you.

Then you'll also have a useless button. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文