打开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目录下(和上面布局中的路径要对应)即可
效果图
当然,上面可以加“N秒后关闭”,“跳过”等等,这是后话