Files
StallDualScreen/app/src/main/java/com/sw/dualscreen/view/CylinderView.kt
T

104 lines
3.7 KiB
Kotlin

package com.sw.dualscreen.view
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.sw.dualscreen.R
class CylinderView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
private val valueText: TextView
private val unitText: TextView
private val cylinderDrawView: CylinderDrawView
private val topImage: ImageView
private val bottomImage: ImageView
private val bottomText: TextView
init {
// 加载内部布局
LayoutInflater.from(context).inflate(R.layout.view_cylinder, this, true)
// 绑定控件
valueText = findViewById(R.id.valueText)
unitText = findViewById(R.id.unitText)
cylinderDrawView = findViewById(R.id.cylinderDrawView)
topImage = findViewById(R.id.topImage)
bottomImage = findViewById(R.id.bottomImage)
bottomText = findViewById(R.id.bottomText)
// 解析自定义属性
context.obtainStyledAttributes(attrs, R.styleable.CylinderView).apply {
// 顶部文字:数值+单位合并
val value = getString(R.styleable.CylinderView_valueText) ?: ""
val unit = getString(R.styleable.CylinderView_unitText) ?: ""
valueText.text = value
valueText.textSize = getDimension(R.styleable.CylinderView_valueTextSize, 20f)
valueText.setTextColor(
getColor(
R.styleable.CylinderView_valueTextColor,
0xFFFFFFFF.toInt()
)
)
unitText.text = unit
unitText.textSize = getDimension(R.styleable.CylinderView_valueTextSize, 10f)
unitText.setTextColor(
getColor(
R.styleable.CylinderView_valueTextColor,
0xFFFFFFFF.toInt()
)
)
// 圆柱属性
cylinderDrawView.cylinderColor =
getColor(R.styleable.CylinderView_cylinderColor, Color.RED)
cylinderDrawView.cylinderWidth =
getDimensionPixelSize(R.styleable.CylinderView_cylinderWidth, 60)
cylinderDrawView.cylinderHeight =
getDimensionPixelSize(R.styleable.CylinderView_cylinderHeight, 180)
getResourceId(R.styleable.CylinderView_topImageSrc, -1).takeIf { it != -1 }?.let {
topImage.setImageResource(it)
// val layoutParams = cylinderDrawView.layoutParams
// layoutParams.height = (layoutParams.width / 2.45).toInt()
// topImage.layoutParams = layoutParams
}
// 底部图片
getResourceId(R.styleable.CylinderView_bottomImageSrc, -1).takeIf { it != -1 }?.let {
bottomImage.setImageResource(it)
// val layoutParams = cylinderDrawView.layoutParams
// layoutParams.height = 44
// bottomImage.layoutParams = layoutParams
}
// 底部文字
bottomText.text = getString(R.styleable.CylinderView_bottomText) ?: "热量"
bottomText.textSize = getDimension(R.styleable.CylinderView_bottomTextSize, 16f)
bottomText.setTextColor(
getColor(
R.styleable.CylinderView_bottomTextColor,
0xFFFFFFFF.toInt()
)
)
recycle() // 回收属性
}
}
fun setToImageSize() {
// topImage.layoutParams = LayoutParams.
}
fun setBottomSize() {
}
}