@@ -3,32 +3,40 @@ package com.lxj.livegiftplayer
33import android.content.Context
44import android.graphics.drawable.Drawable
55import android.util.AttributeSet
6+ import android.util.Log
67import androidx.vectordrawable.graphics.drawable.Animatable2Compat
78import com.github.penfeizhou.animation.webp.WebPDrawable
89
9- class WebpGiftPlayView @JvmOverloads constructor(context : Context , attributeSet : AttributeSet ? = null , defStyleAttr : Int = 0 )
10- : androidx.appcompat.widget.AppCompatImageView (context, attributeSet, defStyleAttr){
11- init {
12- scaleType = ScaleType .CENTER_CROP
13- }
14- fun setDataSource (path : String , onEnd : ()-> Unit ){
15- val webpDrawable = WebPDrawable .fromFile(path)
16- webpDrawable.setLoopLimit(1 )
17- webpDrawable.registerAnimationCallback(object : Animatable2Compat .AnimationCallback (){
18- override fun onAnimationStart (drawable : Drawable ? ) {
19- super .onAnimationStart(drawable)
20- }
21- override fun onAnimationEnd (drawable : Drawable ? ) {
22- super .onAnimationEnd(drawable)
23- onEnd()
24- }
25- })
26- setImageDrawable(webpDrawable)
27- }
10+ class WebpGiftPlayView @JvmOverloads constructor(
11+ context : Context ,
12+ attributeSet : AttributeSet ? = null ,
13+ defStyleAttr : Int = 0
14+ ) : androidx.appcompat.widget.AppCompatImageView(context, attributeSet, defStyleAttr) {
15+ init {
16+ scaleType = ScaleType .CENTER_CROP
17+ }
18+
19+ fun setDataSource (path : String , onEnd : () -> Unit ) {
20+ val webpDrawable = WebPDrawable .fromFile(path)
21+ webpDrawable.setLoopLimit(1 )
22+ webpDrawable.setAutoPlay(false )
23+ webpDrawable.registerAnimationCallback(object : Animatable2Compat .AnimationCallback () {
24+ override fun onAnimationStart (drawable : Drawable ? ) {
25+ super .onAnimationStart(drawable)
26+ }
27+
28+ override fun onAnimationEnd (drawable : Drawable ? ) {
29+ super .onAnimationEnd(drawable)
30+ onEnd()
31+ }
32+ })
33+ setImageDrawable(webpDrawable)
34+ webpDrawable.start()
35+ }
2836
2937 override fun onDetachedFromWindow () {
3038 super .onDetachedFromWindow()
31- if (drawable!= null && drawable is WebPDrawable ){
39+ if (drawable != null && drawable is WebPDrawable ) {
3240 (drawable as WebPDrawable ).apply {
3341 clearAnimationCallbacks()
3442 stop()
0 commit comments