• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

XML fading animation only works for first image? (Kotlin image gallery project)

April 10, 2020 by

Questions › XML fading animation only works for first image? (Kotlin image gallery project)
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I am creating a simple gallery in Kotlin. I am implementing an XML fading effect, so each time the Next or Back button is clicked, the image will fade in for 3 seconds. On the first image it fades in for only half of that time. The next images still have a fade, but only of about .5 seconds. It isn't the XML file because I tested it on another project and it works fine. I believe it has something to do with how I'm integrating it with the loops. You can see I placed the code for the animation in both click listeners. I just started learning both languages two weeks ago, so any advice would help. Thank you.

package com.example.gallery

import android.app.ActionBar.LayoutParams
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageSwitcher
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : Activity() {
    private var iS: ImageSwitcher? = null
    private var btn1: Button? = null
    private var btn2: Button? = null
    val myGallery = intArrayOf(R.drawable.beach_house,R.drawable.cars,R.drawable.moss, 
 R.drawable.pier, R.drawable.tree)
    var i = 0

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

        btn1 = findViewById<View>(R.id.button) as Button
        btn2 = findViewById<View>(R.id.button2) as Button
        iS = findViewById<View>(R.id.imageSwitcher) as ImageSwitcher

        iS!!.setFactory {
            val myView = ImageView(applicationContext)
            myView.scaleType = ImageView.ScaleType.FIT_CENTER
            myView.layoutParams = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT)
            myView
        }

        btn1!!.setOnClickListener {
            val animFadeIn =
                AnimationUtils.loadAnimation(applicationContext, android.R.anim.fade_in)
            iS!!.startAnimation(animFadeIn)

                if (i == myGallery.size) {
                    i = 0
                    imageSwitcher.setImageResource(myGallery[i])
                    i++

                } else{
                    imageSwitcher.setImageResource(myGallery[i])
                    i++
                }
        }

        btn2!!.setOnClickListener {

            val animFadeIn =
                AnimationUtils.loadAnimation(applicationContext, android.R.anim.fade_in)
            iS!!.startAnimation(animFadeIn)

            if (i == myGallery.size) {
                i = 0
                imageSwitcher.setImageResource(myGallery[i])
                i++

            } else{
                imageSwitcher.setImageResource(myGallery[i])
                i++
            }
        }
    }
}
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: android-studio, image-gallery, java, kotlin, xml

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023