恢复活动后停止动画效果

问题描述 投票:-1回答:4

我在视图上添加了一些动画。 启动活动后,所有动画都开始动画。停止动画效果后,我转到同一个应用程序中的另一个活动。当我再次回到动画所在的活动时,所有动画都会再次开始动画,但不是必需的。

我需要在活动中调用resume后停止该动画效果。我找不到任何解决方案。有什么建议?

更新:我添加了所有动画初始化并从onCreate()方法开始。

更新:

动画是在onCreate()方法中启动的

sparkButton.setVisibility(View.GONE);
        welcomeLayout.setVisibility(View.VISIBLE);
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setFillEnabled(true);
        animationSet.setInterpolator(new BounceInterpolator());
        Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.welcome_slide_right_left);

        animation1.setDuration(700);
        animationSet.addAnimation(animation1);

        final AnimationSet animationSet2 = new AnimationSet(true);
        ScaleAnimation animation2 = new ScaleAnimation(1.0f, 0.14f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        animation2.setDuration(400);
        animation2.setStartOffset(400);
        animationSet2.addAnimation(animation2);

        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                welcomeLayout.setAnimation(animationSet2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animationSet2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                welcomeLayout.setVisibility(View.GONE);
                sparkButton.setVisibility(View.VISIBLE);
                sparkButton.playAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        welcomeLayout.setAnimation(animationSet);

我把源码如上所示。这个问题需要澄清,我将把答案标记为黑客解决方案。如果这是我们没有正确处理的Animation对象的问题,那就是我要解决的问题

更新:转到另一个活动的按钮活动

        case R.id.spark_button: {
            // network checking code will append here
            // after that calls the activity
            startActivity(new Intent(SettingsActivity.this, HomeActivity.class));
            break;
        }
android animation onresume
4个回答
1
投票

调用clearAnimation()方法为View,这是onStop()activity方法中的动画,而不是在onResume,因为它也会在活动首次启动时触发


1
投票

您可以尝试使用WorkAround将您的开始动画代码置于onCreate中的此类条件中

if(null == savedBundleState) {
 ////Play your Animation
 }

1
投票

您可以创建布尔值并在活动开始时始终检查它;

boolean shouldShowAnimation = true;   

@Override
public void onResume(){       
    super.onResume();
    if(shouldShowAnimation) {
       shouldShowAnimation = false;
       // Your animation
    }
}

0
投票

在任何名为startAnimation()的View上调用clearAnimation()。

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.