安卓|创建闪屏

启动屏幕通常是应用程序打开时的第一个屏幕。它是一个持续显示特定时间的屏幕,通常在应用程序启动时第一次显示。启动屏幕用于在应用程序完全加载之前显示一些基本的介绍性信息,如公司徽标、内容等。

null

在Android中使用handler创建闪屏

在这里,我们创建了两个活动MainActivity,分别显示启动屏幕和SecondActivity,以便从MainActivity切换到SecondActivity。主程序是用MainActivity编写的,您可以根据需要更改活动。

  • 要删除ActionBar,您需要对样式进行以下更改。xml文件。
    style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
    ...
    
  • 使用适合你的应用的颜色。
  • 无需对清单文件进行任何更改。

    使用“postDelayed()”函数:

    public final boolean postDelayed(Runnable Object token, long delayMillisec)

    此函数将进程延迟指定的时间。这与处理程序一起使用,该处理程序允许您发送和处理与线程的MessageQueue关联的消息和可运行对象。每个处理程序实例都是一个线程。

    下面是创建启动屏幕的代码:

    主要活动。JAVA

    package com.example.hp.splashscreen;
    import android.content.Intent;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.WindowManager;
    public class MainActivity extends AppCompatActivity {
    private static int SPLASH_SCREEN_TIME_OUT= 2000 ;
    #After completion of 2000 ms, the next activity will get started.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super .onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //This method is used so that your splash activity
    //can cover the entire screen.
    setContentView(R.layout.activity_main);
    //this will bind your MainActivity.class file with activity_main.
    new Handler().postDelayed( new Runnable() {
    @Override
    public void run() {
    Intent i= new Intent(MainActivity. this ,
    SecondActivity. class );
    //Intent is used to switch from one activity to another.
    startActivity(i);
    //invoke the SecondActivity.
    finish();
    //the current activity will get finished.
    }
    }, SPLASH_SCREEN_TIME_OUT);
    }
    }

    
    

    主要活动。xml :您可以将任何图像用于启动屏幕,并首先将其粘贴到可绘制文件夹中。XML文件很容易通过拖放方式生成,只需使用imageview并选择合适的图像即可。

    <? xml version = "1.0" encoding = "utf-8" ?>
    < android.support.constraint.ConstraintLayout
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = "com.example.hp.splashscreen.MainActivity" >
    < ImageView
    android:id = "@+id/imageView"
    android:layout_width = "250dp"
    android:layout_height = "200dp"
    app:srcCompat = "@drawable/geeks"
    app:layout_constraintTop_toTopOf = "parent"
    android:layout_marginTop = "8dp"
    android:layout_marginRight = "8dp"
    app:layout_constraintRight_toRightOf = "parent"
    android:layout_marginLeft = "8dp"
    app:layout_constraintLeft_toLeftOf = "parent"
    app:layout_constraintBottom_toBottomOf = "parent"
    android:layout_marginBottom = "8dp"
    app:layout_constraintVertical_bias = "0.447" />
    </ android.support.constraint.ConstraintLayout >

    
    

    输出:

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享