Help us understand the problem. What is going on with this article?

Cyclic property animator in Native View subclass (e.g. Flashing ImageView)

More than 1 year has passed since last update.

We show a method by which native View behavior can be overridden and a property changed in a cyclic sequence. For demonstration, we subclass an ImageView and set its alpha property to cycle between two defined values continuously

First, we define the subclass programmatically

// required constructor signature
class FlashingImageView(context: Context, attrs: AttributeSet?, defStyleAttr: Int): ImageView(context, attrs, defStyleAttr) {

    // define the upper and lower limits to the alpha property
    enum class Alphas(val value: Float) {
        upper(0.25f),
        lower(0.0f)
    }

    // time duration between upper and lower limits 
    val duration: Long = 700L

    // required constructor signatures
    constructor(context: Context) : this(context, null)

    // post a runnable which will start the flashing sequence when this
    // ImageView is ready
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        this.post(object: Runnable {
            override fun run() {
                this@FlashingImageView.startAnimation()
            }
        })
    }

    // define the starting property value and start the flashing sequence
    fun startAnimation() {
        this.setAlpha(Alphas.lower.value)
        this.step()
    }

    // this runnable restarts each step in the flashing sequence
    val runnable = object: Runnable {
        override fun run() {
            this@FlashingImageView.step()
        }
    }

    // swap the target property value for the animation and start 
    // an animation that will call our runnable on completion 
    fun step() {
        val newAlpha = if(this.alpha.equals(Alphas.lower.value))
            Alphas.upper.value
        else
            Alphas.lower.value

        this.animate()
                .alpha(newAlpha)
                .setDuration(duration)
                .withEndAction(runnable)
    }
}

This subclass can be utilized in XML layout as follows:

<path.to.FlashingImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/image"
/>

Note: it is recommended to switch off this type of continuous animation when it is not needed, not only to save CPU, also to avoid errors if attempting to use the Layout Inspector in Android Studio.

cayozin
iOS/Android development (Swift/C++/Java/Kotlin/XML/Javascript), backend (php/mySQL) and Machine Learning (Python/Scala)
ayudante
いつもユーザー中心で技術者とコンサルタントがとことん考え抜く それがアユダンテです
https://ayudante.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした