setBackgroundresource in an ImageButton

0

I want to add a "gif" effect to an imageButton

tiraDado.setBackgroundResource(R.drawable.dado);
    AnimationDrawable frameAnimation = (AnimationDrawable) tiraDado.getBackground();
    frameAnimation.start();

tiraDado is an ImageButton

The problem is that the static image remains in front of the gif effect

    <item android:drawable="@drawable/iconodado" android:duration="1000"/>
    <item android:drawable="@drawable/iconodado2" android:duration="1000"/>
    <item android:drawable="@drawable/iconodado3" android:duration="1000"/>
    
asked by Orz 26.09.2017 в 20:49
source

1 answer

1

If your image is a .gif you can not load it simply by defining the background as the background. In fact, the gif in ImageButton are not supported.

As an option, separate your images in frames and add the images inside the folder /drawable , for example animation.xml :

<animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/frame1" android:duration="2" />
<item android:drawable="@drawable/frame2" android:duration="2" />
<item android:drawable="@drawable/frame3" android:duration="2" />
<item android:drawable="@drawable/frame4" android:duration="2" />
<item android:drawable="@drawable/frame5" android:duration="2" />
<item android:drawable="@drawable/frame6" android:duration="2" />
<item android:drawable="@drawable/frame7" android:duration="2" />
<item android:drawable="@drawable/frame8" android:duration="2" />
<item android:drawable="@drawable/frame9" android:duration="2" />
<item android:drawable="@drawable/frame10" android:duration="2" />
</animation-list>

and in this way load the animation to your ImageButton :

  <ImageButton
        android:id="@+id/imageButton"
        ...
        ...
        android:src="@drawable/animation"/>
    
answered by 27.09.2017 / 01:49
source