|
|
@@ -1,142 +0,0 @@
|
|
|
-package com.adealink.weparty.module.level.view
|
|
|
-
|
|
|
-import android.content.Context
|
|
|
-import android.graphics.LinearGradient
|
|
|
-import android.graphics.Paint
|
|
|
-import android.graphics.Shader
|
|
|
-import android.graphics.Typeface
|
|
|
-import android.text.TextPaint
|
|
|
-import android.util.AttributeSet
|
|
|
-import android.util.LruCache
|
|
|
-import android.view.LayoutInflater
|
|
|
-import android.view.View
|
|
|
-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.weparty.R
|
|
|
-import com.adealink.weparty.commonui.ext.dp
|
|
|
-import com.adealink.weparty.databinding.LevelLayoutUserLevelBinding
|
|
|
-import com.adealink.weparty.module.level.util.getLevelLabelSvgaUrl
|
|
|
-import com.opensource.svgaplayer.SVGADynamicEntity
|
|
|
-import com.opensource.svgaplayer.SVGAParser
|
|
|
-import com.opensource.svgaplayer.SVGAVideoEntity
|
|
|
-import com.opensource.svgaplayer.control.SVGAManager.Companion.parser
|
|
|
-
|
|
|
-/**
|
|
|
- * 1. 用户财富等级标签View,必须指定默认高度, 宽度为wrap_content
|
|
|
- * 2. 里面参数根据设计图大小设置
|
|
|
- */
|
|
|
-class UserWealthLevelView @JvmOverloads constructor(
|
|
|
- context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
|
|
-) : ConstraintLayout(context, attrs, defStyleAttr) {
|
|
|
-
|
|
|
- companion object {
|
|
|
- private val cache = LruCache<String, SVGAVideoEntity>(20) // 缓存SVGAVideoEntity,避免内存过载
|
|
|
- private const val KEY_LEVEL_LABEL_SVGA = "text"
|
|
|
- private const val LEVEL_BELOW_60_WIDTH = 132f
|
|
|
- private const val LEVEL_BELOW_60_HEIGHT = 50f
|
|
|
- private const val LEVEL_EXCEED_60_WIDTH = 143f
|
|
|
- private const val LEVEL_EXCEED_60_HEIGHT = 50f
|
|
|
- }
|
|
|
-
|
|
|
- private val binding = LevelLayoutUserLevelBinding.inflate(LayoutInflater.from(context), this)
|
|
|
-
|
|
|
- var level: Int = 0
|
|
|
-
|
|
|
- init {
|
|
|
- visibility = View.GONE
|
|
|
- }
|
|
|
-
|
|
|
- fun updateLevel(level: Int, quickRecycled: Boolean = true) {
|
|
|
- this.level = level
|
|
|
- if (level <= 0) {
|
|
|
- visibility = View.GONE
|
|
|
- binding.svgaLevelBg.setVideoItem(null, null)
|
|
|
- binding.svgaLevelBg.stopAnimation()
|
|
|
- return
|
|
|
- }
|
|
|
- visibility = View.VISIBLE
|
|
|
- val levelText = getCompatString(R.string.level_user_level, level)
|
|
|
- //根据比例设置宽度
|
|
|
- updateLayoutParams(level)
|
|
|
- val name = getLevelLabelSvgaUrl(level)
|
|
|
- binding.svgaLevelBg.setQuickRecycled(quickRecycled)
|
|
|
- val callback = object : SVGAParser.ParseCompletion {
|
|
|
- override fun onComplete(svgaEntity: SVGAVideoEntity) {
|
|
|
- val textPaint = TextPaint().apply {
|
|
|
- textSize = 56f
|
|
|
- isAntiAlias = true
|
|
|
- textAlign = Paint.Align.CENTER
|
|
|
- typeface = Typeface.DEFAULT_BOLD
|
|
|
- }
|
|
|
- setGradation(textPaint, levelText)
|
|
|
- val dynamicEntity = SVGADynamicEntity().apply {
|
|
|
- setDynamicText(levelText, textPaint, KEY_LEVEL_LABEL_SVGA)
|
|
|
- }
|
|
|
- binding.svgaLevelBg.setVideoItem(svgaEntity, dynamicEntity)
|
|
|
- binding.svgaLevelBg.startAnimation()
|
|
|
- // **解析成功后,将解析结果缓存起来**
|
|
|
- cache.put(name, svgaEntity)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onError(error: Throwable?) {
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- // **先检查缓存,是否已经解析过该资源**
|
|
|
- cache.get(name)?.let {
|
|
|
- callback.onComplete(it) // 直接返回缓存的解析结果
|
|
|
- return
|
|
|
- }
|
|
|
- parser.decodeFromAssets(
|
|
|
- name,
|
|
|
- callback,
|
|
|
- if (level < 60) LEVEL_BELOW_60_WIDTH.dp() else LEVEL_EXCEED_60_WIDTH.dp()
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- private fun updateLayoutParams(level: Int) {
|
|
|
- val viewHeight = layoutParams.height
|
|
|
- if (level < 60) {
|
|
|
- binding.svgaLevelBg.updateLayoutParams {
|
|
|
- width = (viewHeight / LEVEL_BELOW_60_HEIGHT * LEVEL_BELOW_60_WIDTH).toInt()
|
|
|
- height = viewHeight
|
|
|
- }
|
|
|
- } else {
|
|
|
- binding.svgaLevelBg.updateLayoutParams {
|
|
|
- width = (viewHeight / LEVEL_EXCEED_60_HEIGHT * LEVEL_EXCEED_60_WIDTH).toInt()
|
|
|
- height = viewHeight
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private fun setGradation(textPaint: TextPaint, levelText: String) {
|
|
|
- // 设渐变置
|
|
|
- val textWidth = textPaint.measureText(levelText)
|
|
|
- val colorArray = if (level >= 50) {
|
|
|
- intArrayOf(
|
|
|
- getCompatColor(R.color.color_FFFFFF),
|
|
|
- getCompatColor(R.color.color_FFFFECB2),
|
|
|
- )
|
|
|
- } else {
|
|
|
- intArrayOf(
|
|
|
- getCompatColor(R.color.color_FFFFFF),
|
|
|
- getCompatColor(R.color.color_FFDAE5ED),
|
|
|
- )
|
|
|
- }
|
|
|
- textPaint.shader = LinearGradient(
|
|
|
- 0f, 0f, textWidth, 0f, colorArray, floatArrayOf(0f, 1f), Shader.TileMode.CLAMP
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- override fun onAttachedToWindow() {
|
|
|
- super.onAttachedToWindow()
|
|
|
- binding.svgaLevelBg.startAnimation()
|
|
|
- }
|
|
|
-
|
|
|
- override fun onDetachedFromWindow() {
|
|
|
- super.onDetachedFromWindow()
|
|
|
- binding.svgaLevelBg.stopAnimation()
|
|
|
- }
|
|
|
-}
|