欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)歡迎界面停留3秒效果

 更新時間:2021年09月16日 11:10:07   作者:huplion  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)歡迎界面停留3秒效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

0.寫在前面

在這篇教程中來實現(xiàn)一個類似于微信的的延遲3秒再進入主界面的效果。

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)文章

最新評論