📜  如何在Android中创建动画启动画面?

📅  最后修改于: 2021-05-09 17:58:03             🧑  作者: Mango

先决条件:如何使用Kotlin在Android中创建启动画面?

Android启动画面是启动应用程序时用户可见的第一个屏幕。初始屏幕是用户对应用程序的首次体验,因此,它被认为是应用程序中最重要的屏幕之一。它用于显示有关公司徽标,公司名称等的一些信息。我们还可以向“启动画面”中添加一些动画。在本文中,我们将使用Kotlin制作一个动画的启动画面。下面的样本GIF给出得到什么我们将在本文中做的想法

Android中的动画启动画面

创建动画启动画面的步骤

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。

步骤2:创建动画文件

要在android studio中创建动画文件,请仔细按照给定的说明进行操作。转到应用程序> res>右键单击>新建> Android Resource Directory

Android中的动画启动画面

然后将目录名称命名为anim 。然后单击“确定”

Android中的动画启动画面

转到动画>右键单击>新建>动画资源文件

Android中的动画启动画面

并将文件名命名为side_slide ,然后单击OK

Android中的动画启动画面

现在,将此代码添加到动画XML文件中。下面是side_slide.xml文件的代码

XML


  
    
    
  
    


XML


  
    
    
    
      


Kotlin
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.WindowManager
import android.view.animation.AnimationUtils
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
  
@Suppress("DEPRECATION")
class SplashScreen : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)
  
        // This is used to hide the status bar and make 
        // the splash screen as a full screen activity.
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
          
        // HERE WE ARE TAKING THE REFERENCE OF OUR IMAGE 
        // SO THAT WE CAN PERFORM ANIMATION USING THAT IMAGE
        val backgroundImage: ImageView = findViewById(R.id.SplashScreenImage)
        val slideAnimation = AnimationUtils.loadAnimation(this, R.anim.side_slide)
        backgroundImage.startAnimation(slideAnimation)
          
        // we used the postDelayed(Runnable, time) method 
        // to send a message with a delayed time.
        Handler().postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }, 3000) // 3000 is the delayed time in milliseconds.
    }
}


XML


  
    
        
        
            
                
  
                
            
        
    
  


XML


  
    
    
  


Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}


步骤3:建立另一个活动

转到应用程序> Java >第一个程序包名称>右键单击>新建>活动>空活动,然后创建另一个活动并将其命名为SplashScreen 。编辑activity_splash_screen.xml文件,并根据需要在初始屏幕中添加图像和文本。在这里,我们将图像添加到初始屏幕。以下是activity_splash_screen.xml文件的代码

XML格式



  
    
    
    
      

转到SplashScreen.kt文件,并参考以下代码。下面是SplashScreen.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。

科特林

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.WindowManager
import android.view.animation.AnimationUtils
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
  
@Suppress("DEPRECATION")
class SplashScreen : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)
  
        // This is used to hide the status bar and make 
        // the splash screen as a full screen activity.
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
          
        // HERE WE ARE TAKING THE REFERENCE OF OUR IMAGE 
        // SO THAT WE CAN PERFORM ANIMATION USING THAT IMAGE
        val backgroundImage: ImageView = findViewById(R.id.SplashScreenImage)
        val slideAnimation = AnimationUtils.loadAnimation(this, R.anim.side_slide)
        backgroundImage.startAnimation(slideAnimation)
          
        // we used the postDelayed(Runnable, time) method 
        // to send a message with a delayed time.
        Handler().postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }, 3000) // 3000 is the delayed time in milliseconds.
    }
}

步骤4:使用AndroidMenifest.xml文件

转到AndroidMenifest.xml文件,并在“启动屏幕活动”中添加以下代码。这用于隐藏状态栏或操作栏。

另外,在“启动画面活动”中添加以使该活动作为开始活动。因此,无论何时执行该应用程序,用户都可以在开始时看到启动屏幕。以下是AndroidMenifest.xml文件的完整代码

XML格式



  
    
        
        
            
                
  
                
            
        
    
  

步骤5:使用activity_main.xml文件

转到activity_main.xml文件,并添加一个文本,当用户进入MainActivity时,该文本将显示“ Welcome to GeeksforGeeks”。以下是activity_main.xml文件的代码

XML格式



  
    
    
  

步骤6:使用MainActivity.kt文件

MainActivity.kt文件中什么也不做,因为我们已经为“启动画面”创建了一个新活动。下面是MainActivity.kt文件的代码

科特林

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

输出

在Github上找到此项目: https : //github.com/Gauravverma245/AnimatedSplashScreen

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!