android启动界面的两种方式

在做启动页面的时候,想实现和微信,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>

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

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android Studio中创建启动界面有多种方法,以下是其中两种常用的方法: 1. 使用ImageView显示启动界面图片: 在res目录下创建文件夹drawable-hdpi,并将启动界面图片放入该文件夹中。然后在启动界面的布局文件中使用ImageView来显示该图片。例如,创建一个名为activity_splash.xml的布局文件,内容如下: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/splash_image" android:scaleType="centerCrop" /> </RelativeLayout> ``` 其中,@drawable/splash_image是启动界面图片的资源ID。 2. 使用Theme设置启动界面样式: 在res/values/styles.xml文件中定义一个启动界面的主题样式,例如: ```xml <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_image</item> </style> ``` 其中,@drawable/splash_image是启动界面图片的资源ID。 然后,在AndroidManifest.xml文件中将启动界面的主题样式应用到启动的Activity上,例如: ```xml <activity android:name=".SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 其中,SplashActivity是启动界面的Activity类名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值