Android實現(xiàn)歡迎界面停留3秒效果
0.寫在前面
在這篇教程中來實現(xiàn)一個類似于微信的的延遲3秒再進(jìn)入主界面的效果。
1.項目準(zhǔn)備
先新建一個空的android項目。里面只自帶一個MainActivity,首先我們再新建一個Activity叫做WelcomeActivity繼承自Activity。
Activity代碼如下:
//package在此省略,根據(jù)實際自行添加
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);
}
}
布局文件代碼如下:
<?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屬性指定imageView里面要顯示的資源文件的來源路徑,也就是在歡迎界面顯示的圖片,在這里我已經(jīng)預(yù)先上傳了一張圖片了-->
</LinearLayout>
修改清單文件AndroidManifest.xml
聲明WelcomeActivity以及修改Activity的啟動順序,由MainActivity改成WelcomeActivity
原來的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>
修改成
<?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>
至此項目的布局已經(jīng)完成了,現(xiàn)在來完成跳轉(zhuǎn)部分源碼
在這里用到的核心函數(shù)為
Handler.sendEmptyMessageDelayed
主要用來發(fā)送延遲消息
首先新建一個消息處理對象,負(fù)責(zé)發(fā)送與處理消息
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
在handleMessage方法中處理消息,在這里接收到消息不做復(fù)雜處理以后直接執(zhí)行跳轉(zhuǎn)操作
貼上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);
//隱藏標(biāo)題欄以及狀態(tài)欄
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/**標(biāo)題是屬于View的,所以窗口所有的修飾部分被隱藏后標(biāo)題依然有效,需要去掉標(biāo)題**/
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();
}
}
2.總結(jié)
在這里主要利用了android.os.Handler的消息的延遲發(fā)送以及處理。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何通過Battery Historian分析Android APP耗電情況
Android 從兩個層面統(tǒng)計電量的消耗,分別為軟件排行榜及硬件排行榜。它們各有自己的耗電榜單,軟件排行榜為機(jī)器中每個 App 的耗電榜單,硬件排行榜則為各個硬件的耗電榜單。這兩個排行榜的統(tǒng)計是互為獨立,互不干擾的2021-06-06
解決 INSTALL FAILED CONFLICTING PROVIDER的問題方法
這篇文章主要介紹了解決 INSTALL FAILED CONFLICTING PROVIDER的問題方法的相關(guān)資料,需要的朋友可以參考下2017-02-02
android webview中使用Java調(diào)用JavaScript方法并獲取返回值
這篇文章主要介紹了android webview中使用Java調(diào)用JavaScript方法并獲取返回值,本文直接給出代碼示例,需要的朋友可以參考下2015-03-03
詳解Android中的MVP架構(gòu)分解和實現(xiàn)
本篇文章主要介紹了詳解Android中的MVP架構(gòu)分解和實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android 和 windows C/C++/QT通訊時字節(jié)存儲
本篇文章主要介紹 Android和Windows 通訊時數(shù)據(jù)地址的理解,這里提供代碼實例進(jìn)行分析,有需要參考的朋友可以看下2016-07-07
詳解Recyclerview item中有EditText使用刷新遇到的坑
這篇文章主要介紹了詳解Recyclerview item中有EditText使用刷新遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

