~~
 APP的启动图很常见,我们在打开一些app的时候经常先出现一张图(或是广告之类的),过了几秒后才会进入系统相关主页面,这篇就记录一下简单的启动图的处理。
.
1.在你的app项目里新建一个Activity,命名为 SplashActivity,相应的创建对应的布局文件 activity_splash.xml.
.
2.将准备的图片放到mimmap包下,将上面的布局文件背景引用为这张图片1
2
3
4
5
6
7
8
9
10<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".SplashActivity"
    android:background="@mipmap/img_01">
	//background属性来设置图片
</androidx.constraintlayout.widget.ConstraintLayout>
.
修改SplashActivity中的代码如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        Thread myThread=new Thread(){//创建子线程
            @Override
            public void run() {
                try{
                    sleep(3000);//使程序休眠3秒,即3秒后进入系统页面
                    Intent it=new Intent(getApplicationContext(),MainActivity.class);//启动MainActivity
                    startActivity(it);
                    finish();//关闭当前活动
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        myThread.start();//启动线程
    }
}
.
修改配置文件AndroidManifest中的代码,将我们创建的 SplashActivity 设置为首个启动活动。很简单,1
2
3
4
5
6
7
8
9<activity android:name=".SplashActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
       <activity android:name=".MainActivity">
       </activity>