FakeSingleRoomFragment优化

This commit is contained in:
huangjian
2022-07-20 11:28:19 +08:00
parent e2ab878f36
commit e18b5f612c
2 changed files with 8 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
package com.yizhuan.erban.avroom.fragment package com.yizhuan.erban.avroom.fragment
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.view.View
import com.opensource.svgaplayer.SVGADrawable import com.opensource.svgaplayer.SVGADrawable
import com.opensource.svgaplayer.SVGAImageView import com.opensource.svgaplayer.SVGAImageView
import com.opensource.svgaplayer.SVGAParser import com.opensource.svgaplayer.SVGAParser
@@ -19,10 +18,10 @@ class FakeSingleRoomFragment : BaseViewBindingFragment<FragmentFakeSingleRoomBin
private var preRoomInfo: RoomInfo? = null private var preRoomInfo: RoomInfo? = null
private var needJump = false private var needJump = false
private var lastRequestTime = 0L private var lastRequestTime = 0L
private var index = 1
private val minRequestTime = 60 * 1000 private val minRequestTime = 60 * 1000
private val bgPicture = arrayOf("") private val bgPicture = arrayOf("")
private var requesting = false private var requesting = false
private var initBg = false
private val stopRunnable = { private val stopRunnable = {
if (!isDestroyView) { if (!isDestroyView) {
binding.svgaImageViewBg.stopAnimation() binding.svgaImageViewBg.stopAnimation()
@@ -30,22 +29,24 @@ class FakeSingleRoomFragment : BaseViewBindingFragment<FragmentFakeSingleRoomBin
} }
override fun init() { override fun init() {
playSvgaBg(binding.svgaImageViewBg, "svga/single_back.svga")
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
fun requestPreRoomInfo() { fun requestPreRoomInfo() {
if (!initBg) {
playSvgaBg(binding.svgaImageViewBg, "svga/single_back.svga")
initBg = true
}
if (!binding.svgaImageViewBg.isAnimating) { if (!binding.svgaImageViewBg.isAnimating) {
binding.svgaImageViewBg.removeCallbacks(stopRunnable) binding.svgaImageViewBg.removeCallbacks(stopRunnable)
binding.svgaImageViewBg.startAnimation() binding.svgaImageViewBg.startAnimation()
} }
if (CurrentTimeUtils.getCurrentTime() - lastRequestTime < minRequestTime) { if (requesting || CurrentTimeUtils.getCurrentTime() - lastRequestTime < minRequestTime) {
return return
} }
preRoomInfo = null preRoomInfo = null
lastRequestTime = CurrentTimeUtils.getCurrentTime() lastRequestTime = CurrentTimeUtils.getCurrentTime()
requesting = true requesting = true
index++
AvRoomModel.get() AvRoomModel.get()
.nextSingleRoomInfo .nextSingleRoomInfo
.subscribe({ .subscribe({
@@ -84,19 +85,14 @@ class FakeSingleRoomFragment : BaseViewBindingFragment<FragmentFakeSingleRoomBin
} }
private fun playSvgaBg(svgaRoomBg: SVGAImageView, assets: String) { private fun playSvgaBg(svgaRoomBg: SVGAImageView, assets: String) {
svgaRoomBg.clearAnimation() SVGAParser.shareParser().decodeFromAssets(assets, object : SVGAParser.ParseCompletion {
svgaRoomBg.visibility = View.VISIBLE
val mSVGAParser = SVGAParser(svgaRoomBg.context)
mSVGAParser.decodeFromAssets(assets, object : SVGAParser.ParseCompletion {
override fun onComplete(videoItem: SVGAVideoEntity) { override fun onComplete(videoItem: SVGAVideoEntity) {
svgaRoomBg.visibility = View.VISIBLE
val drawable = SVGADrawable(videoItem) val drawable = SVGADrawable(videoItem)
svgaRoomBg.setImageDrawable(drawable) svgaRoomBg.setImageDrawable(drawable)
svgaRoomBg.startAnimation() svgaRoomBg.startAnimation()
} }
override fun onError() { override fun onError() {
svgaRoomBg.visibility = View.GONE
} }
}, null) }, null)

View File

@@ -10,8 +10,7 @@
android:id="@+id/svga_image_view_bg" android:id="@+id/svga_image_view_bg"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:scaleType="centerCrop" android:scaleType="centerCrop" />
app:autoPlay="true" />
</FrameLayout> </FrameLayout>