Android學習之程序啓動畫面

一、實現步驟

1. 創建Android項目

2. 創建一個Empty Activity 作爲主活動。(啓動程序時的第一個活動)BootAnimationActivity

3. 創建一個Empty Activity 作爲啓動畫面消失後跳轉的活動。MainActivity

4. 在res文件夾下創建文件夾drawable-xhdpi,並將想要的畫面圖片放入其中。(不一定要創建,總之放入drawable系列文件夾中,注意不要因爲圖片繪製所需內存過大而閃退)

5. 修改activity_boot_animation.xml 將圖片放入ImageView當中。

6. 修改BootAnimationActivity.java 將狀態欄和標題欄隱藏,在子線程中休眠程序幾秒鐘後跳轉目標活動,並關閉原先活動。

二、實現代碼

1. 效果圖

程序啓動畫面

2. 代碼實現

A.)activity_boot_animation.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"
    tools:context=".BootAnimationActivity">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/animation"
        />

</android.support.constraint.ConstraintLayout>

B.)BootAnimationActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;

public class BootAnimationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在setContentView(R.layout.activity_boot_animation);之前將狀態欄和標題欄隱藏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_boot_animation);

        //新建一個子線程
        Thread thread =new Thread(){
            @Override
            public void run() {
                try{
                    sleep(3000);//程序休眠三秒後啓動MainActivity
                    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(intent);
                    finish();//關閉當前活動,否則按返回鍵還能回到啓動畫面
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        thread.start();//啓動線程
    }
}

參考博客:
https://blog.csdn.net/qq_36455052/article/details/78429713


持續學習Android中,如有錯誤請批評指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章