简体   繁体   中英

Splash screen with fade in animation of ImageView

So i am trying to implement splash screen with image view fade in animation which i want to start simultaneously with the start of the splash screen activity. I also want splash screen activity to end after short delay (on touch event is optional), after animation of image view is finished.

My SplashScreen.java:

package hillbillys.delivery;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

public class SplashScreen extends AppCompatActivity implements Animation.AnimationListener {
protected Animation fadeIn;
protected ImageView img1;
protected ImageView img2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_splash);

    fadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in);

    /*
                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.VISIBLE);
                img1.startAnimation(fadeIn);
                img2.startAnimation(fadeIn);
     */

    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep (2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent intent = new Intent(SplashScreen.this,MainActivity.class);
                startActivity(intent);
            }
        }
    };
    timerThread.start();


}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

@Override
public void onAnimationStart(Animation animation) {


}

@Override
public void onAnimationEnd(Animation animation) {
    Toast.makeText(getBaseContext(), "Animation Stopped!", Toast.LENGTH_SHORT).show();
}

@Override
public void onAnimationRepeat(Animation animation) {

}
}

Application crashes every time i try to add the block of code in comment, no matter where i put it. Without fade in animation works everything just fine. Is there any way how to synchronize these two in easy way? Im quite new with coding so there may be some fatal mistake in what im trying to achieve.

My screeen_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff0d9"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/logo1"
    android:layout_marginTop="250dp"
    android:visibility="gone" />

<ImageView
    android:layout_width="260dp"
    android:layout_height="41dp"
    android:id="@+id/imageView2"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/logo2"
    android:visibility="gone" />

</LinearLayout>

My fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">

<alpha
    android:duration="1000"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0"
    />

</set>

you need to initialize the ImageView before trying to access their properties. Eg

img1 = (ImageView) findViewById(R.id.imageView);
img2 = (ImageView) findViewById(R.id.imageView2);
img1.setVisibility(View.VISIBLE);
img2.setVisibility(View.VISIBLE);
img1.startAnimation(fadeIn);
img2.startAnimation(fadeIn);

Like Blackbelt said you need to initialize,

final img1 = (ImageView) findViewById(R.id.imageView);
final img2 = (ImageView) findViewById(R.id.imageView2);

and if you want to the animation to start correctly after the view is created you need to do this

img1.post(new Runnable(){
    img1.startAnimation(fadeIn);
});

img2.post(new Runnable(){
    img2.startAnimation(fadeIn);
});
Animation animation= AnimationUtils.loadAnimation(this,R.anim.fadeIn);
img1.startAnimation(animation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM