![]() |
VOOZH | about |
Android ImageSwitcher is a user interface widget that provides a smooth transition animation effect to the images while switching between them to display in the view. ImageSwitcher is subclass of View Switcher which is used to animates one image and displays next one. Here, we create ImageSwitcher programmatically in Kotlin file.
First we create a new project by following the below steps:
In this file, we use constraint layout with ImageSwitcher and Buttons.
activity_main.xml:
First, we declare an array which contains the resource of the images used for the ImageView.
private val array = intArrayOf(
R.drawable.grape,
R.drawable.orange,
R.drawable.guava
)
then, we create the ImageSwitcher in the MainActivity.kt file and set ImageView to display the image.
val imgSwitcher = ImageSwitcher(this)
imgSwitcher.setFactory({
val imgView = ImageView(applicationContext)
imgView.scaleType = ImageView.ScaleType.FIT_CENTER
imgView.setPadding(20, 20, 20, 20)
imgView
})
Also, we should add the ImageSwitcher to the layout using.
val layout: ConstraintLayout = findViewById(R.id.main)
layout.addView(imgSwitcher)
MainActivity.kt:
Click next button then we get the other animated image in the View.