0

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 Answers1

0

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

Bhavnik
  • 2,020
  • 14
  • 21
  • Thanks, this solution worked but I was looking to achieve this using fresco – Vanisha Gaba Dec 08 '22 at 05:03
  • Cool, happy to hear that it worked. So you might accept this answer as the right answer or upvote it and afaik fresco does not support the loop feature but you can tweak it with Fresco. – Bhavnik Dec 08 '22 at 05:13