A splash screen or SplashView is common in many apps. In general, SplashView has the following responsibilities:

  • Display SplashView-controllability when appropriate
  • Download, cache, update images
  • The callback responds to the image click event
  • Countdown Dismiss View, actively skip Dissmiss View
  • If there is no local cache, display the default image or do not display the SplashView

<! — more — >

Ok, let’s look at the results:

To display SplashView on an Activity, note that it is called after the Activity setContentView(int viewId) :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        // call after setContentView(R.layout.activity_sample);
        SplashView.simpleShowSplashView(this);
}Copy the code

You can also customize the timeout, default image, callback method, and so on:

/**
     * static method, show splashView on above of the activity
     * you should called after setContentView()
     * @param activity  activity instance
     * @param durationTime  time to countDown
     * @param defaultBitmapRes  if there's no cached bitmap, show this default bitmap;
     *                          if null == defaultBitmapRes, then will not show the splashView
     * @param listener  splash view listener contains onImageClick and onDismiss
     */
SplashView.showSplashView(this, 3, R.drawable.default_img, new SplashView.OnSplashViewActionListener() {
            @Override
            public void onSplashImageClick(String actionUrl) {
                Log.d("SplashView", "img clicked. actionUrl: " + actionUrl);
                Toast.makeText(SampleActivity.this, "img clicked.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSplashViewDismiss(boolean initiativeDismiss) {
                Log.d("SplashView", "dismissed, initiativeDismiss: " + initiativeDismiss);
            }
        });Copy the code

The SplashView data update method updateSplashData can be called anywhere at any time:

// call this method anywhere to update splash view data
SplashView.updateSplashData(this, "http://ww2.sinaimg.cn/large/72f96cbagw1f5mxjtl6htj20g00sg0vn.jpg", "http://jkyeo.com");Copy the code

Talk about the principle. It’s very simple. The only ChildView under FrameLayout is our custom setContentView. So we just need to overlay SplashView as the FrameLayout ChildView at the top. Remove implements the Dismiss when appropriate.

Core code:

ViewGroup contentView = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content); . contentView.addView(splashView, param);Copy the code

All that’s left is to deal with caching, timers, Dismiss animations, displaying hidden status bars, and actionbars.

See the blog post for more details: SplashView- One line of code to solve the splash screen page – Advertising page -Android- article

The corresponding iOS(Swift) version is here: SplashView- One line of code to solve the splash screen page – AD page -iOS-Swift- article