Android---start page + splash screen page

Table of contents

start page

splash page


 

start page

When the app enters the home page, it will load a picture online and then enter the splash screen page. In this way, a good experience can be given to the user.

Function : avoid loading white screen pages, and perform business preprocessing (network detection, data preloading...)

Interface composition : Logo, background image, version number, etc.

Step 1 : Create a new style under src-->main-->res-->values-->themes-->themes.xml. Set startup page

    <!-- 设置启动页的 style -->
    <style name="Theme.Pager" parent="Theme.ScreenAdapter" >
        <item name="android:windowBackground">@drawable/dp</item> // 设置启动页背景图片
        <item name="android:windowFullscreen">true</item> // 设置背景图片为全屏显示
        <item name="android:navigationBarColor">@android:color/transparent</item> // 设置导航栏透明

    </style>

Step 2 : Set this theme in AndroidManifest.xml

Step 3 : If we just set it like this, we will find that after the startup page is completed and the home page is reached, the background image still exists, so we need to reset the theme of the app to the original one. So, we set the original theme in the onCreate method.


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_StartPagerTest); // 设置为原主题
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

splash page

Role : advertising, marketing promotion.

Interface composition : advertisement, skip (countdown button).

Step 1 : Create a new SplashActivity.java. And set the startup page as SplashAcrivity in AndroidManifest.xml.

 Step 2 : activity_splash.xml

There is a Button in the layout, which is used to realize the function of clicking "skip". The splash screen page can be skipped early during the countdown.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@drawable/img"
    tools:context=".SplashActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="50dp"
        android:textColor="@color/white"
        android:text="My App"
        tools:ignore="HardcodedText,SpUsage"
        android:gravity="center"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳过"
        android:layout_gravity="right"
        android:layout_marginTop="35dp"
        android:layout_marginRight="35dp"
        tools:ignore="HardcodedText,RtlHardcoded" />

</FrameLayout>

Step 3 : SplashActivity.java

package com.example.screenadapter;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.widget.Button;

public class SplashActivity extends AppCompatActivity {

    private Button button; // "跳过" 按钮
    private Handler handler = new Handler();
    TimeCount timeCount;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            toMainActivity();
        }
    };

    /**
     *TODO 闪屏页 --》 最开始功能界面(首页面)
     */
    private void toMainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_ScreenAdapter);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        initViews();
        //延迟 3s 发送,即当用户不主动点击跳过时,3s 后自动结束
        handler.postDelayed(runnable, 3000);

        timeCount = new TimeCount(4000, 1000);
        timeCount.start();
    }

    /**
     * 初始化 View
     */
    private void initViews() {
        button = findViewById(R.id.button);
        button.setOnClickListener(v -> {
            toMainActivity();
        });
    }

    // 倒计时计时器
    class TimeCount extends CountDownTimer{


        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long l) {
            // 设置倒计时
            button.setText(l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            // 移除掉 runnable 方法
            handler.removeCallbacks(runnable);
        }
    }
}

Among them, the toMainActivity() method realizes jumping from SplashActivity (splash screen page) to MainActivity (home page). We trigger it on the button (skip) button and when the "countdown" is over. When the user clicks the "Skip" button, the countdown can end early and enter the home page, or it can automatically jump to the home page when the countdown ends.

Guess you like

Origin blog.csdn.net/qq_44950283/article/details/130302030