为自己的APP添加启动页

打开APP的时候会有一个启动过程 ,为了较好的体验,一般加载一个启用页(大部分可能是广告······)。 最熟悉的打开微信时候一个地球图片,这篇文章简单介绍怎么加载该图片。

添加布局文件

解决思路:定义一个linearLayout、设置背景即可。2秒钟后隐藏该界面,加载MainActivity。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/launch"
    >
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/launchImg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/launch">
    </LinearLayout>
</LinearLayout>

定义启动Activity

设置隐藏时间(2000ms),时间到动画结束时候,隐藏该布局,打开MainActivity

package com.mos.weather;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/11/5.
 */

public class LaunchActivity extends Activity {
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载启动图片
        setContentView(R.layout.activity_launch);
        animationLaunch();
    }
    private void animationLaunch() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 1.0f);
        alphaAnimation.setDuration(2000);
        linearLayout = findViewById(R.id.launchImg);
        linearLayout.setAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                linearLayout.setVisibility(View.VISIBLE);
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                linearLayout.setVisibility(View.GONE);  
                Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
                startActivity(intent);
                //结束当前的 Activity
                LaunchActivity.this.finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        }
        );
    }
}

修改默认加载Activity

修改AndroidMenifest.xml中启动时候加载的主Activity为上面定义的LaunchActivity

<activity android:name=".LaunchActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
</activity>

启动图

把准备好的launch.png图片扔到drawable目录下(和上面布局中的路径要对应)即可

img_6d77915e478cc560ec2acec58bbf119e.png
启动图

效果图

img_18f5bdb0e3edfcdd397b02ca8b86dcc0.jpe
S71108-214255.jpg

当然,上面可以加“N秒后关闭”,“跳过”等等,这是后话

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值