[6.7.0][车速] 化化车速控件渲染逻辑

This commit is contained in:
renwj
2024-09-30 11:07:13 +08:00
parent c6cce01d8d
commit 386136e649
2 changed files with 46 additions and 17 deletions

View File

@@ -1,38 +1,66 @@
package com.mogo.eagle.core.function.hmi.bone.status.speed
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.Shader.TileMode.CLAMP
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatTextView
import com.mogo.eagle.core.function.hmi.R
import com.mogo.eagle.core.utilcode.kotlin.scope
import kotlinx.android.synthetic.main.layout_speed_view.view.tv_speed
import kotlinx.coroutines.launch
import com.mogo.eagle.core.utilcode.util.UiThreadHandler
class SpeedView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
LinearLayout(context, attrs, defStyleAttr) {
class SpeedView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr) {
companion object {
private const val TAG = "SpeedView"
}
private val speedTextView: GradientTextView
init {
LayoutInflater.from(context).inflate(R.layout.layout_speed_view, this, true)
speedTextView = findViewById(R.id.tv_speed)
}
fun updateSpeed(speed: Int) {
tv_speed?.post {
tv_speed?.also {
Log.d(TAG, "--- (${it.width},${it.height})")
it.paint.shader = LinearGradient(it.width.toFloat() / 2.0f, 0f, it.width.toFloat() / 2.0f, it.height.toFloat(), Color.parseColor("#19FFCB"), Color.parseColor("#1970FF"), CLAMP)
it.text = "$speed"
}
UiThreadHandler.post {
speedTextView.text = "$speed"
}
}
class GradientTextView : AppCompatTextView {
private var mPrevWidth: Int = -1
private var mPrevHeight: Int = -1
companion object {
private const val TAG = "GradientTextView-SpeedView"
private val START_COLOR = Color.parseColor("#19FFCB")
private val END_COLOR = Color.parseColor("#1970FF")
}
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
Log.d(TAG, "--- onSizeChanged --:[$w, $h]")
if (mPrevWidth != w || mPrevHeight != h) {
mPrevWidth = w
mPrevHeight = h
paint?.shader = LinearGradient(w / 2.0f, 0f, w / 2f, h.toFloat(), START_COLOR, END_COLOR, CLAMP)
}
}
override fun onDraw(canvas: Canvas?) {
val current = text
if (current != null && !TextUtils.isEmpty(current)) {
paint?.also {
canvas?.drawText(current.toString(), paddingStart.toFloat(), baseline.toFloat(), it)
}
}
}
}
}

View File

@@ -6,13 +6,14 @@
tools:layout_width="wrap_content"
tools:layout_height="@dimen/dp_175">
<TextView
<view class="com.mogo.eagle.core.function.hmi.bone.status.speed.SpeedView$GradientTextView"
android:id="@+id/tv_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/dp_150"
android:fontFamily="@font/font_din"
tools:text="60"/>
tools:text="60"
tools:ignore="SpUsage" />
<TextView
android:layout_width="wrap_content"