You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.2 KiB

package sgd.and04.layout
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageSwitcher
import android.widget.ImageView
import android.view.animation.AnimationUtils
import android.widget.Button
class ImageSwitcherActivity : AppCompatActivity() {
private val flowers = intArrayOf(R.drawable.flower1, R.drawable.flower2,
R.drawable.flower4)
private var index = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_switcher)
// create the ImageSwitcher
val imgSwitcher = ImageSwitcher(this)
imgSwitcher?.setFactory({
val imgView = ImageView(applicationContext)
imgView.scaleType = ImageView.ScaleType.CENTER
imgView.setPadding(20, 20, 20, 20)
imgView.setAdjustViewBounds(true)
imgView.setMaxHeight(2400)
imgView
})
val c_Layout = findViewById<FrameLayout>(R.id.scrollView)
//add ImageSwitcher in constraint layout
c_Layout?.addView(imgSwitcher)
// set the method and pass array as a parameter
imgSwitcher?.setImageResource(flowers[index])
val imgIn = AnimationUtils.loadAnimation(
this, android.R.anim.slide_in_left)
imgSwitcher?.inAnimation = imgIn
val imgOut = AnimationUtils.loadAnimation(
this, android.R.anim.slide_out_right)
imgSwitcher?.outAnimation = imgOut
// previous button functionality
val prev = findViewById<Button>(R.id.prev)
prev.setOnClickListener {
index = if (index - 1 >= 0) index - 1 else 1
imgSwitcher?.setImageResource(flowers[index])
}
// next button functionality
val next = findViewById<Button>(R.id.next)
next.setOnClickListener {
index = if (index + 1 < flowers.size) index +1 else 0
imgSwitcher?.setImageResource(flowers[index])
}
}
fun onButtonExitClick(view: View) {
startActivity(Intent(this, MainActivity::class.java))
}
}