Search code examples
javaandroidanimationautomationsplash-screen

Frame-by-Frame Animated Splash-Screen


[Android Studio] I'm trying to get a few frames to display quickly one after another and then change to a different activity automatically on startup.

I'm able to make the splash activity wait the appropriate amount of time before proceeding, but the animation still isn't playing.

I'm using this person's method (https://www.youtube.com/watch?v=lGRWYJlS3d0) to animate the frames and display them to an ImageView - in the example, the animation is controlled by a stop and start button which sets the frames to loop endlessly (or not), however I would just like it to be activated and displayed when the app is launched.

The way I'm currently implementing this method is causing the app to crash once particular lines are read, but I'm probably just doing it wrong so any help will be appreciated. Thanks. :)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.company.framebyframe">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.HOME" />

                <category android:name="android.intent.category.MAIN" />
            </intent-filter>
        </activity>
    </application>
</manifest>

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/splash_layout"
    android:background="@drawable/custom_animation">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

custom_animation.xml under drawables

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/image0" android:duration="250"/>
    <item android:drawable="@drawable/image1" android:duration="100"/>
    <item android:drawable="@drawable/image2" android:duration="100"/>
    <item android:drawable="@drawable/image3" android:duration="100"/>
    <item android:drawable="@drawable/image4" android:duration="750"/>
    <item android:drawable="@drawable/image3" android:duration="750"/>
    <item android:drawable="@drawable/image4" android:duration="750"/>
    <item android:drawable="@drawable/image3" android:duration="500"/>
    <item android:drawable="@drawable/image0" android:duration="5000"/>
</animation-list>

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

    AnimationDrawable anim;
    ImageView imageView;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imageView = (ImageView) findViewById(R.id.imageView);

        imageView.setBackgroundResource(R.drawable.custom_animation);

        anim = (AnimationDrawable) imageView.getBackground();

        anim.start();

//  //  start new activity after 3.5 seconds  //  //

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startNextActivity();
            }
        }, 3500);
    }

    public void startNextActivity() {
        if (isFinishing())
            return;
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

crashes tend to occur when i start unquoting these lines:

imageView = (ImageView) findViewById(R.id.imageView);

imageView.setBackgroundResource(R.drawable.custom_animation);

PS: if there are any naming mistakes, it might be due to me trying to make the filenames more generic and understandable, but feel free to point out anything you believe might cause problems.


Solution

  • You need to set the layout on the activity class, otherwise the application won't know what layout if should inflate. That's the main reason your application crashes when you try to find the image view, because you haven't set the content layout. Add this line just after super.onCreate and before you try to manipulate any UI element

    setContentView(R.layout.activity_splash);
    

    Also, remove the background declaration on the constraint layout, you're already loading the animated drawable to an image view

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/splash_layout">  
    
        ... 
    
    </android.support.constraint.ConstraintLayout>