|
|
@@ -12,7 +12,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
|
|
|
import androidx.core.view.updateLayoutParams
|
|
|
import com.adealink.frame.aab.util.getCompatColor
|
|
|
import com.adealink.frame.aab.util.getCompatString
|
|
|
-import com.adealink.frame.util.DisplayUtil
|
|
|
+import com.adealink.frame.log.Log
|
|
|
import com.adealink.weparty.R
|
|
|
import com.adealink.weparty.commonui.ext.dp
|
|
|
import com.adealink.weparty.databinding.LevelLayoutUserLevelBinding
|
|
|
@@ -24,92 +24,94 @@ import com.opensource.svgaplayer.control.SVGAManager.Companion.parser
|
|
|
|
|
|
/**
|
|
|
* 1. 用户等级View,必须指定默认高度, 宽度为wrap_content
|
|
|
- * 2. 要旧svga,不要使用新svga,会有问题
|
|
|
*/
|
|
|
class UserLevelView @JvmOverloads constructor(
|
|
|
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
|
|
) : ConstraintLayout(context, attrs, defStyleAttr) {
|
|
|
|
|
|
companion object {
|
|
|
- private val svgaCache = LruCache<String, SVGAVideoEntity>(20) // 缓存SVGAVideoEntity,避免内存过载
|
|
|
-
|
|
|
- private val DEFAULT_TEXT_SIZE = 66f
|
|
|
- private val DEFAULT_HEIGHT = DisplayUtil.dp2px(49f).toFloat() //标准高度
|
|
|
+ private val svgaCache = LruCache<String, SVGAVideoEntity>(20)
|
|
|
+ private const val KEY_LEVEL_LABEL_SVGA = "text"
|
|
|
+ private const val DEFAULT_TEXT_SIZE = 66f
|
|
|
}
|
|
|
|
|
|
- val binding = LevelLayoutUserLevelBinding.inflate(LayoutInflater.from(context), this)
|
|
|
+ private val binding = LevelLayoutUserLevelBinding.inflate(LayoutInflater.from(context), this)
|
|
|
+
|
|
|
+ var level: Int = 0
|
|
|
|
|
|
init {
|
|
|
visibility = View.GONE
|
|
|
- binding.svgaLevelBg.clearsAfterDetached = false
|
|
|
- binding.svgaLevelBg.setQuickRecycled(false)
|
|
|
- binding.svgaLevelBg.setAutoPlay(true)
|
|
|
+ binding.svgaLevelBg.apply {
|
|
|
+ clearsAfterDetached = false
|
|
|
+ setQuickRecycled(false)
|
|
|
+ setAutoPlay(true)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private val KEY_LEVEL_LABEL_SVGA = "text"
|
|
|
- var level: Int = 0
|
|
|
-
|
|
|
fun updateLevel(level: Int) {
|
|
|
this.level = level
|
|
|
+
|
|
|
if (level <= 0) {
|
|
|
visibility = View.GONE
|
|
|
- binding.svgaLevelBg.setVideoItem(null, null)
|
|
|
- binding.svgaLevelBg.stopAnimation()
|
|
|
- } else {
|
|
|
- visibility = View.VISIBLE
|
|
|
- val levelText = getCompatString(R.string.level_user_level, level)
|
|
|
-
|
|
|
- //根据比例设置宽度
|
|
|
- if (level < 60) {
|
|
|
- val viewHeight = layoutParams.height
|
|
|
- binding.svgaLevelBg.updateLayoutParams {
|
|
|
- width = (viewHeight / 100f * 265).toInt()
|
|
|
- height = viewHeight
|
|
|
- }
|
|
|
- } else {
|
|
|
- val viewHeight = layoutParams.height
|
|
|
- binding.svgaLevelBg.updateLayoutParams {
|
|
|
- width = (viewHeight / 100f * 286).toInt()
|
|
|
- height = viewHeight
|
|
|
- }
|
|
|
+ binding.svgaLevelBg.apply {
|
|
|
+ setVideoItem(null, null)
|
|
|
+ stopAnimation()
|
|
|
}
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- val name= getLevelLabelSvgaRes(level = level)
|
|
|
- val callback=object : SVGAParser.ParseCompletion {
|
|
|
- override fun onComplete(videoItem: SVGAVideoEntity) {
|
|
|
- val dynamicEntity = SVGADynamicEntity()
|
|
|
- val textPaint = TextPaint().apply {
|
|
|
- textSize = DEFAULT_TEXT_SIZE
|
|
|
- color = getCompatColor(R.color.color_FFFFF979)
|
|
|
- textAlign = Paint.Align.CENTER
|
|
|
- typeface = Typeface.DEFAULT_BOLD
|
|
|
- }
|
|
|
+ visibility = View.VISIBLE
|
|
|
|
|
|
- dynamicEntity.setDynamicText(levelText, textPaint, KEY_LEVEL_LABEL_SVGA)
|
|
|
- binding.svgaLevelBg.setVideoItem(videoItem, dynamicEntity)
|
|
|
- binding.svgaLevelBg.startAnimation()
|
|
|
+ val levelText = getCompatString(R.string.level_user_level, level)
|
|
|
+ val svgaName = getLevelLabelSvgaRes(level)
|
|
|
+ val viewHeight = layoutParams.height
|
|
|
|
|
|
- // **解析成功后,将解析结果缓存起来**
|
|
|
- svgaCache.put(name, videoItem)
|
|
|
- }
|
|
|
+ // 设置 svga 背景宽高
|
|
|
+ val svgaWidth = if (level < 60) (viewHeight / 100f * 265).toInt()
|
|
|
+ else (viewHeight / 100f * 286).toInt()
|
|
|
|
|
|
- override fun onError(error: Throwable?) {
|
|
|
+ binding.svgaLevelBg.updateLayoutParams {
|
|
|
+ width = svgaWidth
|
|
|
+ height = viewHeight
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ // 优先尝试缓存
|
|
|
+ svgaCache.get(svgaName)?.let {
|
|
|
+ setSVGAWithText(it, levelText)
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
+ val parserCallback = object : SVGAParser.ParseCompletion {
|
|
|
+ override fun onComplete(svgaEntity: SVGAVideoEntity) {
|
|
|
+ setSVGAWithText(svgaEntity, levelText)
|
|
|
+ svgaCache.put(svgaName, svgaEntity)
|
|
|
}
|
|
|
|
|
|
- // **先检查缓存,是否已经解析过该资源**
|
|
|
- svgaCache.get(name)?.let {
|
|
|
-// Log.d("TAG", "updateLevel: cache,${svgaCache.size()}")
|
|
|
- callback.onComplete(it) // 直接返回缓存的解析结果
|
|
|
- return
|
|
|
+ override fun onError(error: Throwable?) {
|
|
|
+ error?.printStackTrace()
|
|
|
+ Log.e("UserLevelView", "SVGA parse error", error)
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ val svgaSize = if (level < 60) 132.dp() else 140.dp()
|
|
|
+ parser.decodeFromAssets(svgaName, parserCallback, svgaSize)
|
|
|
+ }
|
|
|
|
|
|
+ private fun setSVGAWithText(videoItem: SVGAVideoEntity, levelText: String) {
|
|
|
+ val textPaint = TextPaint().apply {
|
|
|
+ textSize = DEFAULT_TEXT_SIZE
|
|
|
+ color = getCompatColor(R.color.color_FFFFF979)
|
|
|
+ textAlign = Paint.Align.CENTER
|
|
|
+ typeface = Typeface.DEFAULT_BOLD
|
|
|
+ }
|
|
|
+
|
|
|
+ val dynamicEntity = SVGADynamicEntity().apply {
|
|
|
+ setDynamicText(levelText, textPaint, KEY_LEVEL_LABEL_SVGA)
|
|
|
+ }
|
|
|
|
|
|
- parser.decodeFromAssets(
|
|
|
- getLevelLabelSvgaRes(level = level),callback, if (level < 60) 132.dp() else 140.dp()
|
|
|
- )
|
|
|
+ binding.svgaLevelBg.apply {
|
|
|
+ setVideoItem(videoItem, dynamicEntity)
|
|
|
+ startAnimation()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -117,5 +119,4 @@ class UserLevelView @JvmOverloads constructor(
|
|
|
super.onAttachedToWindow()
|
|
|
binding.svgaLevelBg.startAnimation()
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
+}
|