Skip to content Skip to sidebar Skip to footer

How Can I Create Animated Togglebutton?

Yes, I can create ToggleButton with 2 pictures(On, Off) But I want to create a ToggleButton with 3-5 pictures. For example, when is it OFF and I click: Off picture Middle picture

Solution 1:

EDIT:

You can use Frame Animation: In res/drawable/myanim.xml:

<?xml version="1.0" encoding="utf-8"?><animation-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/pic_one"android:duration="50"/><itemandroid:drawable="@drawable/pic_two"android:duration="50" /><itemandroid:drawable="@drawable/pic_three"android:duration="50" /></animation-list>

You can then use this animation as a plain drawable:

<ImageView android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/myanim"/>

To start the animation you do

AnimationDrawablebackgroundDrawable= (AnimationDrawable) image.getDrawable();
backgroundDrawable.start();

You can also use a Value Animator. I haven't tested this, but you should be able to put something like this into onClick handler of you button:

int[] backgrounds = ...;//ids of the backgrounds for the buttonValueAnimatoranim= ValueAnimator.ofInt(0, backgrounds.length);
anim.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
    @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
        inti= (Integer) animation.getAnimatedValue();
        intbackgroundId= backgrounds[i];
        yourButton.setBackgroundResource(backgroundId);
    }
});
anim.setDuration(500); //0.5 seconds
anim.start();

Post a Comment for "How Can I Create Animated Togglebutton?"