Splash screen in Android Application

前端 未结 3 1137
轮回少年
轮回少年 2020-12-16 07:09

I am modifying an open source application and want to add a splash screen to it, Can some one help me in it?

When the application starts a black screen appears for 2

相关标签:
3条回答
  • 2020-12-16 07:35

    Don't forget to consider that the user might want to quit your app before the splash-delay is over. So clear any pending runnables/messages when the user exits your app.

    Example can be found here

    0 讨论(0)
  • 2020-12-16 07:46

    Splash activity

    public class LaunchScreen extends Activity {
    
      public static final long TIME = 3000;
    
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logo);
    
        Protocol.getInstance(this);
    
        Thread welcomeThread = new Thread() {
    
            @Override
            public void run() {
                try {
                    sleep(TIME);
                } catch (Exception e) {
                    Log.e(getClass().getName(), e.toString());
                } finally {
                    startActivity(new Intent(LaunchScreen.this,MainScreen.class));
                    finish();
                }
            }
        };
        welcomeThread.start();
      }
    }
    

    logo.xml file:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
    >
    <ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo"
    android:layout_centerInParent="true"
    >
    </ImageView>
    </RelativeLayout>
    

    in AndroidManifest :

    activity android:name=".LaunchScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainScreen" android:label="@string/app_name" ></activity>
    
    0 讨论(0)
  • 2020-12-16 07:47

    Use class SplashScreen as under

    public class Splashscreen extends Activity {
    
    private static final int SPLASH_DISPLAY_TIME = 3000; /* 3 seconds */
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        new Handler().postDelayed(new Runnable() {
    
            public void run() {
    
                Intent mainIntent = new Intent(Splashscreen.this,
                        MainActivity.class);
                Splashscreen.this.startActivity(mainIntent);
    
                Splashscreen.this.finish();
                overridePendingTransition(R.anim.mainfadein,
                        R.anim.splashfadeout);
            }
        }, SPLASH_DISPLAY_TIME);
    }
    

    }

    **Add mainfadein.xml & splashfadeout.xml in res->anim folder

    mainfadein.xml**

        <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:fromAlpha="0.0"
            android:toAlpha="1.0"
            android:duration="1000">
    </alpha>
    

    splashfadeout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:zAdjustment="top"
            android:fromAlpha="1.0"
            android:toAlpha="0.0"
            android:duration="1000" >
    </alpha>
    

    and add splash.xml just Add an ImageView and set its background as screen & add image of urchoice in layout

    And make Splashscreen class as Launcher and make all other class as HOME in manifest file

    0 讨论(0)
提交回复
热议问题