Android深入

Android 中的 Lottie 动画入门

2022-07-17  本文已影响0人  安安_660c

增强应用程序的用户体验是任何开发人员的首要任务之一。与图像和文本描述相比,为某些描述提供动画总是可以增强应用程序的用户体验!

Lottie是用于移动应用程序的库之一,有助于以更简单的方式提供动画。如何开始在 Android 中使用 Lottie 动画?让我们深入阅读这篇文章并理解这一点。

今天,我们将学习并构建它。


与往常一样,我们将在示例项目的帮助下快速理解这一点。

创建项目

让我们在应用级别的 build.gradle 文件中添加所需的 Lottie 动画依赖项:

//Lottie Animation
implementation 'com.airbnb.android:lottie:3.4.0'

在继续项目之前,我们必须从https://lottiefiles.com/中选择所需的动画。我们可以在搜索栏中输入类别,选择相应的动画,然后下载文件的 JSON 版本。


现在在我们的项目中创建一个资产文件夹。

项目结构中的assets目录应该放在src目录下。

将所有必需的下载 JSON文件添加到此资产文件夹。


现在,一旦我们创建了项目,我们就知道我们有两个文件 MainActivity.kt 和 activity_main.kt。

让我们从我们的activity_main.kt文件开始:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_headline"
        app:layout_constraintBottom_toTopOf="@+id/lav_main"
        android:layout_marginTop="16dp"
        android:text="Steps to follow during this CoronaVirus Quarantine!"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="center"
        android:textColor="@android:color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lav_main"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:lottie_autoPlay="true"
        app:lottie_fileName="fight_coronavirus.json"
        app:lottie_loop="false"
        app:lottie_speed="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/layout_click"
        app:layout_constraintTop_toBottomOf="@id/tv_headline" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_click"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/lav_main">

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lav_click_left"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            app:lottie_autoPlay="true"
            android:visibility="gone"
            app:lottie_fileName="left_arrow.json"
            app:lottie_loop="true"
            app:lottie_speed="1" />

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lav_click_right"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="100dp"
            app:lottie_autoPlay="true"
            app:lottie_fileName="right_arrow.json"
            app:lottie_loop="true"
            app:lottie_speed="1" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

在此示例中,我们有一个基本 UI,其中包含

我们可以从 activity_main.xml 中看到,需要添加到 Lottie Animation 视图中的文件是通过属性完成的:

//The json file added to the assets directory
app:lottie_fileName="filename.json"

现在,让我们更新MainActivity.kt文件:

package com.mindorks.lottieanimation

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.lav_click_left
import kotlinx.android.synthetic.main.activity_main.lav_click_right
import kotlinx.android.synthetic.main.activity_main.lav_main
import kotlinx.android.synthetic.main.activity_main.tv_headline

class MainActivity : AppCompatActivity() {

    private var count: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initListeners()
    }

    private fun initListeners() {
        lav_click_right.setOnClickListener {
            count++
            showStep(count = if (count > 4 ) 4 else count)
        }
        lav_click_left.setOnClickListener {
            count--
            showStep(count = if (count < 0 ) 0 else count)
        }
    }

    private fun showStep(count: Int) {
        when (count) {
            0 -> {
                setFooter(
                    isLeftVisible = false,
                    isRightVisible = true
                )
                setStepContent(
                    header = "Steps to follow during this CoronaVirus Quarantine!",
                    lottieAnimationFile = "fight_coronavirus.json"
                )
            }
            1 -> {
                setFooter(
                    isLeftVisible = true,
                    isRightVisible = true
                )
                setStepContent(header = "1\. Maintain Social Distancing!", lottieAnimationFile = "social_distancing.json")
            }
            2 -> {
                setFooter(
                    isLeftVisible = true,
                    isRightVisible = true
                )
                setStepContent(header = "2\. Stay Home, Stay Safe!", lottieAnimationFile = "stay_safe.json")
            }
            3 -> {
                setFooter(
                    isLeftVisible = true,
                    isRightVisible = true
                )
                setStepContent(header = "3\. Wash/Sanatize your hands!", lottieAnimationFile = "sanatize.json")
            }
            4 -> {
                setFooter(
                    isLeftVisible = true,
                    isRightVisible = false
                )
                setStepContent(header = "4\. Learn/Upgrade your skill set!", lottieAnimationFile = "learn.json")
            }
        }
    }

    private fun setStepContent(header: String, lottieAnimationFile: String) {
        tv_headline.text = header
        lav_main?.apply {
            setAnimation(lottieAnimationFile)
            repeatCount = 5
            playAnimation()
        }
    }

    private fun setFooter(
        isLeftVisible: Boolean,
        isRightVisible: Boolean
    ) {
        lav_click_left?.apply {
            visibility = if (isLeftVisible) View.VISIBLE else View.GONE
        }
        lav_click_right?.apply {
            visibility = if (isRightVisible) View.VISIBLE else View.GONE
        }

    }
}

理解示例中的逻辑

lav_click_right.setOnClickListener {
            count++
            showStep(count = if (count > 4 ) 4 else count)
        }
        lav_click_left.setOnClickListener {
            count--
            showStep(count = if (count < 0 ) 0 else count)
        }

private fun showStep(count: Int)

private fun setStepContent(header: String,  lottieAnimationFile:String)

private fun setFooter(isLeftVisible: Boolean,isRightVisible:Boolean)

我们现在都准备好了代码。让我们在任何设备上运行这个应用程序,看看 Lottie Animation 是如何工作的!

LottieAnimationViews 属性

LottieAnimation 视图有很多属性,我们可以通过这些属性控制视图的动画。项目中使用的一些是

app:lottie_autoPlay="true"

app:lottie_loop="false"

app:lottie_speed="1"

接下来是什么?

您可以克隆此项目并尝试以下操作:

还有很多...

作者:Dheeraj Sree
链接:Getting started with Lottie Animation in Android

上一篇 下一篇

猜你喜欢

热点阅读