安卓中的视图动画师示例
ViewAnimator 是一个非常迷人和有用的功能,因为它可以在两个或多个视图之间平滑切换,主要用于屏幕上视图的动画功能。它是*和*视图切换器的父类,主要区别是它还可以在 2 个以上的视图之间切换。它是框架布局容器的子类。以下是定义视图动画器的方法:
可扩展标记语言
<ViewAnimator
    android:id="@+id/simpleViewAnimator1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--   You need to add views here  -->
</ViewAnimator>
动画意味着显然一个时刻只有一个视图是活动的,因此有许多重要的方法可以使流程流畅。
重要方法
*视图动画器的属性*
例子
下面给出了一个 GIF 示例,来了解一下在这篇文章中要做什么。请注意,我们将使用 Kotlin 语言来实现该项目。

逐步实施
*第一步:创建新项目*
要在安卓工作室创建新项目,请参考如何在安卓工作室创建/启动新项目。注意选择科特林作为编程语言。
*步骤 2:使用 activity_main.xml 文件*
转到 activity_main.xml 文件,参考以下代码。下面是 activity_main.xml 文件的代码。
可扩展标记语言
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical">
    <ViewAnimator
        android:id="@+id/simpleViewAnimator1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ViewAnimator>
    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:background="#055"
        android:text="NEXT"
        android:textColor="#fff"
        android:textStyle="bold" />
</LinearLayout>
*第三步:使用 MainActivity.kt 文件*
转到 MainActivity.kt 文件,参考以下代码。下面是 MainActivity.kt 文件的代码。代码中添加了注释,以更详细地理解代码。
我的锅
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import android.widget.ViewAnimator
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
    private var simpleViewAnimator1: ViewAnimator? = null
    var buttonNext: Button? = null
    // array of images, here taking metal images
    var availableImages = intArrayOf(R.drawable.gold, R.drawable.silver, R.drawable.platinum,
            R.drawable.copper, R.drawable.aluminium)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // get The references of Button and ViewAnimator
        buttonNext = findViewById<View>(R.id.btnNext) as Button
        // get the reference of ViewAnimator
        simpleViewAnimator1 = findViewById<View>(R.id.simpleViewAnimator1) as ViewAnimator
        for (i in availableImages.indices) {
            // create a new object  for ImageView by this way
            val imgView = ImageView(applicationContext)
            // Let us set image resource for ImageView
            imgView.setImageResource(availableImages[i])
            // Then add the child view in ViewAnimator
            simpleViewAnimator1!!.addView(imgView)
        }
        // Declare in and out animations and load them using AnimationUtils class
        val animationIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
        val animationOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
        // set the animation type to ViewAnimator
        simpleViewAnimator1!!.inAnimation = animationIn
        simpleViewAnimator1!!.outAnimation = animationOut
        // set false value for setAnimateFirstView, but this is ultimately your choice
        simpleViewAnimator1!!.animateFirstView = false
        // Let us write ClickListener for NEXT button
        // The current view will go out and next view will come in with
        // specified animation
        buttonNext!!.setOnClickListener {
            // TODO Auto-generated method stub
            // show the next view of ViewAnimator `     `
            simpleViewAnimator1!!.showNext()
        }
    }
}
输出
为上面的小演示代码附上一个短视频。这里有 5 种不同的金属可供选择,在每种视图中,我们可以看到一个又一个视图。你也可以通过在你的安卓应用中使用这个功能来获得乐趣。

