Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

Im new to creating app using java on android studio. Im creating splash screen but not works as i expected

The splash screen should move towards up and down, then go to the next screen. however, it just change the screen normally. the problem i think maybe between the handler and the animate splashscreen. this is my splash.java. hoping anyone can help thank you.

package com.example.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;

public class Splash extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;

    ImageView logo,splashImg;
    TextView appName;
    LottieAnimationView lottieAnimationView;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        logo = findViewById(R.id.logo);
        appName = findViewById(R.id.appname);
        splashImg = findViewById(R.id.img);
        lottieAnimationView = findViewById(R.id.lottie);


        splashImg.animate().translationY(-1600).setDuration(1000).setStartDelay(3000);
        logo.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
        appName.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
        lottieAnimationView.animate().translationY(1400).setDuration(1000).setStartDelay(3000);

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);

    }

}
question from:https://stackoverflow.com/questions/65918196/im-new-to-creating-app-using-java-on-android-studio-im-creating-splash-screen-b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like you start another Activity at the same time you play your animation. Notice the .setStartDelay(3000); at the end of each of your animation calls.

Just removing this will work. However, there are better ways to handle this.

One of the ways would be to override the onAnimationEnd method of your longest animation so that it either changes the Activity immediately or the change is delayed starting from that point in time. This will look something like this:

splashImg.animate().translationY(-1600).setDuration(1000).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            Intent i = new Intent(Splash.this, MainActivity.class);
            startActivity(i);
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...