Glide callback after success in Kotlin

前端 未结 5 1769
孤独总比滥情好
孤独总比滥情好 2021-02-12 23:37
   private SimpleTarget target = new SimpleTarget() {  

    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
             


        
5条回答
  •  攒了一身酷
    2021-02-13 00:20

    Android Studio 3.5 - Kotlin 1.3.41 - Glide 4.9.0

    • add this dependency to your build.gradle under dependencies:

      implementation 'com.github.bumptech.glide:glide:4.9.0'

    Go to the top of your class and add these imports (pay attention expecially to target class which is different from the kotlin target class annotation):

    import com.bumptech.glide.Glide
    import com.bumptech.glide.load.DataSource
    import com.bumptech.glide.load.engine.DiskCacheStrategy
    import com.bumptech.glide.load.engine.GlideException
    import com.bumptech.glide.load.resource.gif.GifDrawable
    import com.bumptech.glide.request.RequestListener
    import com.bumptech.glide.request.target.Target
    import android.graphics.drawable.Drawable
    import android.support.graphics.drawable.Animatable2Compat
    

    I've put some extra parameter as override(600, 600), if you don't need remove it..

    // Start animation
            Glide
                .with(this)
                .load(R.drawable.tossina_pose1)
                .centerCrop()
                .override(600, 600)
                .placeholder(R.drawable.tossina_idle_0)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .listener(object : RequestListener {
                    override fun onLoadFailed(p0: GlideException?, p1: Any?, p2: Target?, p3: Boolean): Boolean {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }
                    override fun onResourceReady(p0: Drawable?, p1: Any?, p2: Target?, p3: DataSource?, p4: Boolean): Boolean {
                        (p0 as GifDrawable).setLoopCount(1)
                        p0.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
                            override fun onAnimationEnd(drawable: Drawable) {
                                println("animation ends")
                            }
                        })
                        return false
                    }
                })
                .into(img)
    

    Some indications: R.drawable.tossina_pose1 is my GIF, you can put also gif local image like this example. The final line .into(img) have img that is my imageView

提交回复
热议问题