android启动界面的两种方式

时间:2022-12-24 16:42:26

在做启动页面的时候,想实现和微信,qq那样的启动页面,在第一次启动会显示出来,按下返回键回到桌面再次进入就不会显示了,除非程序被杀死。昨天学习到了两种方式实现这个效果,记下

1.设置一个logo页面,添加要显示的Image,在创建的线程中执行跳转操作,延时3秒,代码如下

MainActiviy.java
package com.example.administrator.logopage;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
public void run() {
/* Create an Intent that will start the Main WordPress Activity. */
Intent mainIntent = new Intent(MainActivity.this, Main2Activity.class);
MainActivity.this.startActivity(mainIntent);
MainActivity.this.finish();
}
}, 3000);
}

}
在跳转到的Main2Activity中添加按键监听
 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
Intent i= new Intent(Intent.ACTION_MAIN); //主启动,不期望接收数据

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //新的activity栈中开启,或者已经存在就调到栈前

i.addCategory(Intent.CATEGORY_HOME); //添加种类,为设备首次启动显示的页面

startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
按下返回就会跟按下home键操作一致,效果自行尝试。

2.直接在主页中实现,这样可能会好些,代码如下

package com.example.administrator.logopage;

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

public class Main2Activity extends Activity {
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
AlphaAnimation animation=new AlphaAnimation(1.0f,1.0f);
animation.setDuration(2000);
linearLayout= (LinearLayout) findViewById(R.id.ll);
linearLayout.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
linearLayout.setVisibility(View.VISIBLE);
}

@Override
public void onAnimationEnd(Animation animation) {
linearLayout.setVisibility(View.GONE); //隐藏起来,不需要任何布局空间
}

@Override
public void onAnimationRepeat(Animation animation) {
//linearLayout.setVisibility(View.GONE);
}
}
);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
Intent i= new Intent(Intent.ACTION_MAIN); //主启动,不期望接收数据

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //新的activity栈中开启,或者已经存在就调到栈前

i.addCategory(Intent.CATEGORY_HOME); //添加种类,为设备首次启动显示的页面

startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
}

在布局文件的前面添加了一个线形布局,放入要设置的启动image,给他设置了一个2秒的动画,监听动画结束时,设置他为隐藏。也为它设置了返回按钮的监听,设置和home功能相同布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.logopage.Main2Activity">
<LinearLayout
android:orientation="vertical"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher">
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="过来了"/>
</LinearLayout>

效果是显示两秒的背景同,然后文本视图就出来了。