Jim
Jim

Reputation: 149

ScaleAnimation duplicate as background

I want to shrink a button/checkbox based on an animation. For this I use the following code and this works well:

                val scaler = ScaleAnimation(
                    1.0f,
                    0.0f,
                    1.0f,
                    1.0f,
                    Animation.RELATIVE_TO_SELF,
                    0.0f,
                    Animation.RELATIVE_TO_SELF,
                    0.5f,
                )
                
                scaler.duration = 10000
                kitchen.startAnimation(scaler)

However, I would like the button/checkbox to create itself as a 2nd layer and then run the animation. So that you have an overlay on the "normal button" that is then behind it. How do you do this?

Upvotes: 0

Views: 141

Answers (1)

Cheticamp
Cheticamp

Reputation: 62831

If I understand you question, the easiest way to accomplish what you want is to create two buttons - one atop the other. The one underneath would normally be invisible but would be made visible when the animation starts on the top button. Here is an example:

enter image description here

XML for the layout:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonUnder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/buttonOver"
        app:layout_constraintEnd_toEndOf="@id/buttonOver"
        app:layout_constraintStart_toStartOf="@id/buttonOver"
        app:layout_constraintTop_toTopOf="@id/buttonOver" />

    <Button
        android:id="@+id/buttonOver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

and the code:

val buttonUnder = binding.buttonUnder
buttonUnder.isVisible = false
val buttonOver = binding.buttonOver
buttonOver.setOnClickListener {
    val scaler = ScaleAnimation(
        1.0f,
        0.0f,
        1.0f,
        1.0f,
        Animation.RELATIVE_TO_SELF,
        0.0f,
        Animation.RELATIVE_TO_SELF,
        0.5f,
    )
    buttonUnder.isVisible = true
    scaler.duration = 10000
    buttonOver.startAnimation(scaler)
}

Upvotes: 2

Related Questions