Skip to content

Commit 48036ac

Browse files
committed
修复WebpGiftPlayView播放到一半退到后台回来重新播放的问题
1 parent 08b07cd commit 48036ac

4 files changed

Lines changed: 35 additions & 24 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GiftPlayer/src/main/java/com/lxj/livegiftplayer/WebpGiftPlayView.kt

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@ package com.lxj.livegiftplayer
33
import android.content.Context
44
import android.graphics.drawable.Drawable
55
import android.util.AttributeSet
6+
import android.util.Log
67
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
78
import 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()

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ dependencies {
3434
implementation 'androidx.core:core-ktx:1.3.2'
3535
implementation 'androidx.appcompat:appcompat:1.2.0'
3636
implementation 'com.blankj:utilcodex:1.30.6'
37+
implementation 'com.github.penfeizhou.android.animation:awebp:2.12.0'
3738
implementation project(path: ':GiftPlayer')
3839
}

app/src/main/java/com/lxj/livegiftplayerdemo/MainActivity.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.lxj.livegiftplayerdemo
22

3+
import android.graphics.drawable.Drawable
34
import androidx.appcompat.app.AppCompatActivity
45
import android.os.Bundle
56
import android.util.Log
67
import android.view.View
7-
import android.widget.FrameLayout
8-
import android.widget.TextClock
9-
import android.widget.TextView
8+
import android.widget.*
9+
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
1010
import com.blankj.utilcode.util.FileUtils
1111
import com.blankj.utilcode.util.LogUtils
1212
import com.blankj.utilcode.util.ResourceUtils
1313
import com.blankj.utilcode.util.ToastUtils
14+
import com.github.penfeizhou.animation.webp.WebPDrawable
1415
import com.lxj.alphaplayer.view.AlphaPosition
1516
import com.lxj.livegiftplayer.GiftFileType
1617
import com.lxj.livegiftplayer.GiftPlayItem
1718
import com.lxj.livegiftplayer.LiveGiftPlayer
19+
import com.lxj.livegiftplayer.WebpGiftPlayView
1820

1921
class MainActivity : AppCompatActivity() {
2022
override fun onCreate(savedInstanceState: Bundle?) {

0 commit comments

Comments
 (0)