Android启动页面(闪屏页面)的实现

xiaoxiao2021-02-28  19

闪屏页面是指APP刚启动时的页面会自动跳转到主页面

单单实现闪屏页面非常简单。

闪屏界面的作用: 1.展示自己软件的logo,口号标识语等, 2.作为广告平台,获取利益 3.加载下一页面(其他Activity或全局)所需要的数据 4.检查更新 效果展示 首先目录结构 SplashActivity

package com.hxh.splashactivitydemo; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.RelativeLayout; public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置没有标题栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_splash); RelativeLayout layoutSplash=(RelativeLayout) findViewById(R.id.activity_splash); AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f); alphaAnimation.setDuration(1000);//设置动画播放时长1000毫秒(1秒) layoutSplash.startAnimation(alphaAnimation); //设置动画监听 alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } //动画结束 @Override public void onAnimationEnd(Animation animation) { //页面的跳转 Intent intent=new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); } @Override public void onAnimationRepeat(Animation animation) { } }); } }

MainActivity主界面

package com.hxh.splashactivitydemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.Window; /** * Created by HuXiaoHui on 2017/10/9. */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去掉标题栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } }

activity_splash.xml文件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_splash" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash_background" tools:context="com.hxh.splashactivitydemo.SplashActivity"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:text="SplashDemo" android:textSize="50dp" android:textColor="#fff" /> </RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="欢迎进入主界面" android:textSize="30dp" android:textColor="#f00" /> </RelativeLayout>

最后还需要更改AndroidManifest.xml

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <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> </application>

首先在AndroidManifest.xml中注册新加入的SplashActivity。 然后将的内容放在SplashActivity下。 意思就是让SplashActivity的内容先启动。然后再跳转到MainActivity

需要下载源码的可以点击这里链接

转载请注明原文地址: https://www.6miu.com/read-1100274.html

最新回复(0)