You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按时间间隔展示启动页视图并在最后打开新Activity?

Solution: Trigger Activity Navigation After Last View Animation Completes

Hey there! The issue with your current code is that you're using a fixed timeout to navigate to MainActivity, which might not align perfectly with when your last view finishes animating. Instead, we can hook into the animation's end event to trigger the navigation exactly when the final view is visible and its animation completes. Here's the adjusted code and breakdown:

Modified Code

public class SplashActivity extends AppCompatActivity {
    private TextView welcome_tv1, welcome_tv2, welcome_tv3;
    private ImageView logo;
    private Typeface typeface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        init();
        setAnimations();
        // Removed the fixed timeout postDelayed here
    }

    private void setAnimations() {
        // Animate logo
        YoYo.with(Techniques.FadeInDown)
                .duration(2500)
                .repeat(0)
                .playOn(logo);
        logo.setVisibility(View.VISIBLE);

        // Animate first text view
        YoYo.with(Techniques.FadeInDown)
                .repeat(0)
                .delay(400)
                .duration(1000)
                .playOn(welcome_tv1);
        welcome_tv1.setVisibility(View.VISIBLE);

        // Animate second text view
        YoYo.with(Techniques.FadeInDown)
                .repeat(0)
                .delay(1400)
                .duration(1000)
                .playOn(welcome_tv2);
        welcome_tv2.setVisibility(View.VISIBLE);

        // Animate third text view, with end listener to trigger navigation
        YoYo.with(Techniques.FadeInDown)
                .repeat(0)
                .delay(2800)
                .duration(800)
                .listener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {}

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        // This runs ONLY when the last animation finishes
                        navigateToMainActivity();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {}

                    @Override
                    public void onAnimationRepeat(Animator animation) {}
                })
                .playOn(welcome_tv3);
        welcome_tv3.setVisibility(View.VISIBLE);
    }

    private void navigateToMainActivity() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

    private void init() {
        welcome_tv1 = findViewById(R.id.welcome_tv_one);
        welcome_tv2 = findViewById(R.id.welcome_tv_two);
        welcome_tv3 = findViewById(R.id.welcome_tv_three);
        typeface = Typeface.createFromAsset(getApplicationContext().getAssets(), "Fonts/myFont.ttf");
        logo = findViewById(R.id.logo);
        setTextViews();
    }

    private void setTextViews() {
        welcome_tv1.setText(getResources().getString(R.string.welcome_tv_txt));
        welcome_tv2.setText(getResources().getString(R.string.welcome_tv2_txt));
        welcome_tv3.setText(getResources().getString(R.string.welcome_tv3_txt));
        welcome_tv1.setTypeface(typeface);
        welcome_tv2.setTypeface(typeface);
        welcome_tv3.setTypeface(typeface);
    }
}

Key Changes Explained

  • Removed fixed timeout: We got rid of the handler.postDelayed with SPLASH_TIME_OUT because it doesn't account for animation timing variations—if you adjust animation durations later, you'd have to recalculate the timeout manually.
  • Added animation end listener: For your final view (welcome_tv3), we attached an AnimatorListener that calls navigateToMainActivity() only when the animation finishes. This guarantees navigation happens right after the last view is fully visible and its animation completes.
  • Extracted navigation to a method: navigateToMainActivity() makes the code cleaner and easier to maintain if you need to add pre-navigation logic (like checking user sessions) later.

Bonus Tip

If you ever need to tweak the duration or delay of any animations, this setup will automatically adapt—no need to adjust any timeout values.

内容的提问来源于stack exchange,提问作者Harry Roberts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:09:47