📜  Android中的ViewAnimator示例

📅  最后修改于: 2021-05-13 15:49:01             🧑  作者: Mango

ViewAnimator是一个非常有趣且有用的功能,它可以平滑地在两个或多个视图之间切换,并且主要用于屏幕上视图的动画功能。它是ViewFlipper和ViewSwitcher的父类,主要区别是它还可以在两个以上的视图之间切换。它是FrameLayout容器的子类。以下是定义ViewAnimator的方法:

XML

  
    
  


XML

  
    
    
      
    


Kotlin
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(R.id.btnNext) as Button
  
        // get the reference of ViewAnimator
        simpleViewAnimator1 = findViewById(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()
        }
    }
}


动画显然意味着一次只能激活一个视图,因此有许多重要的方法可以使流程顺畅。

重要方法

Methods

Description

 showNext()

The name of the method is self-explanatory. This is used to show the 

next view of ViewAnimator. Only one view can be active at the moment.

 showPrevious()

The name of the method is self-explanatory. This is used to show the

 previous view of ViewAnimator. Only one view can be active at the moment.

addView(View child)

At run time, if we want to add a view, we can use this. 

Add the child view at run time in the ViewAnimator.

setInAnimation(in) Set the animation of the appearance of the object on the screen
setOutAnimation(out)

Opposite of  setInAnimation(). The previous one is removed by using

an animation set with the setOutAnimation() method, and then 

places the new one using the animation set by the setInAnimation() method.

 getCurrentView() Currently displayed child view of ViewAnimator.
getDisplayedChild() Index for current displayed child view of ViewAnimator.
getInAnimation()

Current animation used to animate a View that enters the screen 

can be got by this method. This method returns the animation

that we set using the setInAnimation() method.

 getOutAnimation()

Current animation used to animate a View that exits the screen

can be got by this method. This method returns the out animation that we set using setoutAnimation() method.

removeAllViews() To remove all child views from the ViewGroup.
 removeView(View view)

To remove the child view of ViewAnimator. We can do that bypassing the

child view which we want to remove.

removeViewAt(int index)

 If there is a requirement like to remove a view at the specified position

in the group, we can use this.

setDisplayedChild(int whichChild) To set the index of current displayed child view of ViewAnimator
setAnimateFirstView(boolean animate)

The current view should be animated the first time in the ViewAnimator

can be displayed to either true or false value.

getAnimateFirstView() If we have set the current view animated to true/false.

ViewAnimator的属性

Attributes

Description

id To uniquely identify a ViewAnimator.
animateFirstView If we want to set the current view as animated, we can have this attribute
inAnimation The identifier for the animation to use when a view is shown
outAnimation The identifier for the animation to use when a view is hidden
padding set the padding from the left, right, the top, or bottom side of a ViewAnimator.

例子

下面给出了一个示例GIF,以使我们对本文中要做的事情有一个了解。请注意,我们将使用Kotlin语言实施此项目。

Android示例GIF中的ViewAnimator

分步实施

步骤1:创建一个新项目

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

步骤2:使用activity_main.xml文件

转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。

XML格式


  
    
    
      
    

步骤3:使用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(R.id.btnNext) as Button
  
        // get the reference of ViewAnimator
        simpleViewAnimator1 = findViewById(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种不同的金属可供使用,在每种视图下,我们可以一个视图接一个视图。您也可以通过在Android应用中使用此功能来获得乐趣。

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