android:使用时间表调用动画时出现问题

发布于 2024-10-02 13:53:16 字数 1600 浏览 9 评论 0原文

当我的应用程序加载时,我让它对屏幕进行动画处理。在 onAnimationEnd 中,我调用 Schedule 让它在 2 秒后调用一个函数,该函数将调用一个动画来再次将视图动画化。目前它已开始动画化,但调度程序似乎从未调用过该动画。如果我单击主页,然后返回应用程序,它会立即调用动画。如果计划正常运行,为什么应用程序会等到失去焦点然后重新获得焦点才能实际触发计划应该调用的函数。代码如下:

这些是在 onCreate 中定义的:

slideTopIn = AnimationUtils.loadAnimation(this, R.anim.slide_top_in);
slideTopIn.setAnimationListener(slideTopInListener);
slideTopOut = AnimationUtils.loadAnimation(this, R.anim.slide_top_out);
slideTopOut.setAnimationListener(slideTopOutListener);

freeAlertMsg.startAnimation(slideTopIn);

这些是在 Activity 类的 onCreate 之外定义的:

private AnimationListener slideTopInListener = new AnimationListener(){
    public void onAnimationEnd(Animation anim){
        alertTimer.schedule(new Runnable() {
            public void run() { hideFreeAlert(); }
        }, 2, TimeUnit.SECONDS);
    };
    public void onAnimationStart(Animation anim){
        freeAlertMsg.setVisibility(View.VISIBLE);
    };
    public void onAnimationRepeat(Animation anim){

    };
};

private AnimationListener slideTopOutListener = new AnimationListener(){
    public void onAnimationEnd(Animation anim){
        mainScreen.removeView(freeAlert);
    };
    public void onAnimationStart(Animation anim){
        Log.d("Animation Listener","slideTopOutListener");
    };
    public void onAnimationRepeat(Animation anim){

    };
};

private void hideFreeAlert(){
    freeAlertMsg.startAnimation(slideTopOut);
}

所以基本上它执行 startAnimation(slideTopIn)。然后什么也没有发生。如果我单击主页,然后返回应用程序,则 startAnimation(slideTopOut) 立即执行。我猜计划实际上会运行,但由于某种原因从未触发动画。

When my app loads I have it animate a screen in. In the onAnimationEnd I call schedule to have it call a function 2 seconds later that will call an animation to animate the view out again. Currently it animates in, but the scheduler never seems to call the animate out. If I click home, then go back to the app it immediately calls the animate out. Why would, if the schedule is working, the app wait until it loses focus then gets focus back to actually trigger the function the schedule is supposed to call. Here's the code:

These are defined in the onCreate:

slideTopIn = AnimationUtils.loadAnimation(this, R.anim.slide_top_in);
slideTopIn.setAnimationListener(slideTopInListener);
slideTopOut = AnimationUtils.loadAnimation(this, R.anim.slide_top_out);
slideTopOut.setAnimationListener(slideTopOutListener);

freeAlertMsg.startAnimation(slideTopIn);

These are defined outside the onCreate within the activity class:

private AnimationListener slideTopInListener = new AnimationListener(){
    public void onAnimationEnd(Animation anim){
        alertTimer.schedule(new Runnable() {
            public void run() { hideFreeAlert(); }
        }, 2, TimeUnit.SECONDS);
    };
    public void onAnimationStart(Animation anim){
        freeAlertMsg.setVisibility(View.VISIBLE);
    };
    public void onAnimationRepeat(Animation anim){

    };
};

private AnimationListener slideTopOutListener = new AnimationListener(){
    public void onAnimationEnd(Animation anim){
        mainScreen.removeView(freeAlert);
    };
    public void onAnimationStart(Animation anim){
        Log.d("Animation Listener","slideTopOutListener");
    };
    public void onAnimationRepeat(Animation anim){

    };
};

private void hideFreeAlert(){
    freeAlertMsg.startAnimation(slideTopOut);
}

So basically it executes the startAnimation(slideTopIn). Then nothing happens. If i click home, then go back to the app, the startAnimation(slideTopOut) immediately executes. I'm guessing the schedule actually runs but never triggers the animation for some reason.

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

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

发布评论

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

评论(1

雨轻弹 2024-10-09 13:53:16

只需在任何 View 上调用 postDelayed() 即可。

Just call postDelayed() on any View.

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