如何按时间间隔展示启动页视图并在最后打开新Activity?
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.postDelayedwithSPLASH_TIME_OUTbecause 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 anAnimatorListenerthat callsnavigateToMainActivity()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
相关产品推荐
相关产品推荐

