How to show Gif animation only once then show placeholder image in version 2.6.0 android

284 Views Asked by At

I am using fresco 2.6.0 library for loading a gif animation into imageView, our requirement is to play animation just once and then show placeholder or source image in that image view. We don't want to use glide as the animations looks disturbed.

tried creating a controller and then passing it to view, but not getting when to call anim.stop()


 val gifController: DraweeController = Fresco.newDraweeControllerBuilder()
                .setUri(UriUtil.getUriForResourceId(R.drawable.save_draft))
                .setControllerListener(object : BaseControllerListener<ImageInfo?>() {
                    override fun onFinalImageSet(
                        id: String,
                        imageInfo: ImageInfo?,
                        anim: Animatable?
                    ) {
                        if (anim != null) {
                            anim.start()
                        }

                    }
                })
                .build()
            iv.controller = gifController


1

There are 1 best solutions below

2
Bhavnik On BEST ANSWER

Well, I haven't used fresco for GIF loading, but you can try the below code and it is working pretty well actually.

class Temp: AppCompatActivity(), AnimationListener {

    private lateinit var gifImageView: GifImageView
    private lateinit var gifDrawable: GifDrawable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.temp)

        gifImageView = findViewById(R.id.gifImage)


        try {
            gifDrawable = GifDrawable(resources, R.drawable.anim_1)
            gifDrawable.loopCount = 5 // set your loop animation count to 1

            gifDrawable.addAnimationListener(this@Temp)
        } catch (e: Exception) {
            Timber.e("Exception :: ${e.localizedMessage}")
        }

        gifImageView.setImageDrawable(gifDrawable)
    }

    override fun onAnimationCompleted(loopNumber: Int) {
        Timber.e("Animation count :: $loopNumber")
        if (loopNumber == 4)
             gifImageView.setImageResource(R.mipmap.ic_launcher)
        }
        // perform your necessary logic here when the loopNumber
        matches with the one that you had defined above (i.e 
        gifDrawable.loopCount = 4)
}

Library used :

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.25'

Output :

Ouput