Android realizes the effect of staying in the welcome interface for 3 seconds
- 2021-08-17 01:01:48
- OfStack
0. Write in front
In this tutorial, we will achieve an effect similar to WeChat, which delays 3 seconds before entering the main interface.
1. Project preparation
Create an empty android project first. There is only one MainActivity in it. First, we will build a new Activity called WelcomeActivity inherited from Activity.
The Activity code is as follows:
//package Omitted here, add it according to the actual situation
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
/**
* Created by HUPENG on 2016/9/21.
*/
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
}
The layout file code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:src="@mipmap/welcome"/>
<!--android src Property specifies imageView The source path of the resource file to be displayed, that is, the picture displayed in the welcome interface, where I have uploaded it in advance 1 A picture -->
</LinearLayout>
Modify manifest file AndroidManifest. xml
Declare WelcomeActivity and modify the startup sequence of Activity from MainActivity to WelcomeActivity
Original xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="supershare.android.hupeng.me.supershare">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Modify to
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="supershare.android.hupeng.me.supershare">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>
At this point, the layout of the project has been completed. Now, let's complete the jump part of the source code
The core functions used here are
Handler.sendEmptyMessageDelayed
Mainly used to send delayed messages
First, create a new message processing object, which is responsible for sending and processing messages
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
In the handleMessage method, the message is processed, where the jump operation is directly performed after receiving the message without complicated processing
Stick up all the codes of WelcomeActivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.Window;
import android.view.WindowManager;
/**
* Created by HUPENG on 2016/9/21.
*/
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the title bar and status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/** The title belongs to View So the title is still valid after all the decorations of the window are hidden , Need to remove the title **/
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
handler.sendEmptyMessageDelayed(0,3000);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
getHome();
super.handleMessage(msg);
}
};
public void getHome(){
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
STEP 2 Summarize
Here, we mainly use the delayed sending and processing of android. os. Handler messages.