恢复活动后停止动画效果

时间:2019-04-03 12:23:18

标签: android animation onresume

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

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

更新: 我添加了所有动画init并从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);

我把源如上所示。 这个问题需要澄清,我将答案标记为hack解决方案。如果这是我们未能正确处理的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;
        }

4 个答案:

答案 0 :(得分:1)

您可以尝试使用WorkAround将开始的动画代码放在onCreate这样的条件中

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

答案 1 :(得分:1)

您可以创建布尔值,并始终在活动开始时对其进行检查;

boolean shouldShowAnimation = true;   

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

答案 2 :(得分:0)

在您调用startAnimation()的任意一个View上调用clearAnimation()。

希望这会有所帮助!

答案 3 :(得分:0)

CachingConnectionFactory调用import React, { Component } from 'react'; export default class Todo extends Component { state = { nom: '', age: '', ville: '', items: [], start: 0 }; onChange = (event) => { this.setState({ [event.target.name]: event.target.value }); } onSubmit = (event) => { event.preventDefault(); this.setState({ nom: '', age: '', ville: '', items: [...this.state.items, this.state.nom, this.state.age, this.state.ville], start: 1 }); } renderCard = () => { return this.state.items.map((item, index) => { return ( <div className="card" key={index}> {item.nom} {item.age} {item.ville} </div> ); }); } render() { return ( <div> <div className="card mb-3"> <div className="card-header">Ajouter une Personne</div> <div className="card-body"> <form onSubmit={this.onSubmit}> <div className="form-group"> <label htmlFor="name">Nom</label> <input type="text" className="form-control form-control-lg" name="nom" value={this.state.nom} onChange={this.onChange} /> </div> <div className="form-group"> <label htmlFor="name">Age</label> <input type="text" className="form-control form-control-lg" name="age" value={this.state.age} onChange={this.onChange} /> </div> <div className="form-group"> <label htmlFor="name">Ville</label> <input type="text" className="form-control form-control-lg" name="ville" value={this.state.ville} onChange={this.onChange} /> </div> <button className="btn btn-primary btn-block">Créez votre Fiche !</button> </form> </div> </div> {this.renderCard()} </div> ); } } 方法,该方法正在您的clearAnimation()的{​​{1}}方法的动画中而不是在View中进行动画处理,因为它也会在活动启动时触发第一次

相关问题