cantona_7
cantona_7

Reputation: 1177

Animating just the color of the button

I am animating button which creates a blink effect. It works fine. This is my code.

    animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(800);
    animation.setStartOffset(30);
    animation.setRepeatMode(Animation.REVERSE);
    animation.setRepeatCount(Animation.INFINITE);

But this makes the button completely vanish and start again. I am looking to animate only the color of the button. When it animates the button should be visible with a background-color and only color changes ? Any ideas to proceed forward ?

Upvotes: 0

Views: 36

Answers (1)

Jignesh Mayani
Jignesh Mayani

Reputation: 7203

Try this

In drawable, abc.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:drawable="@drawable/color_red"
    android:duration="500" />
<item
    android:drawable="@drawable/color_green"
    android:duration="500" />

In color.xml

<drawable name="color_green">#008000</drawable>
    <drawable name="color_red">#FF0000</drawable>

In main.xml

<Button
android:background="@drawable/abc"/>

In Java,

 AnimationDrawable animationDrawable = (AnimationDrawable) buttonView.getBackground();
        animationDrawable.setEnterFadeDuration(500);
        animationDrawable.setExitFadeDuration(500);
        animationDrawable.start();

Upvotes: 1

Related Questions