diff --git a/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/bone/status/speed/SpeedView.kt b/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/bone/status/speed/SpeedView.kt index 4fb37b98e6..5d969efeb5 100644 --- a/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/bone/status/speed/SpeedView.kt +++ b/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/bone/status/speed/SpeedView.kt @@ -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) + } + } + } + } } \ No newline at end of file diff --git a/core/function-impl/mogo-core-function-hmi/src/main/res/layout/layout_speed_view.xml b/core/function-impl/mogo-core-function-hmi/src/main/res/layout/layout_speed_view.xml index e631fa1cd7..da4215c02a 100644 --- a/core/function-impl/mogo-core-function-hmi/src/main/res/layout/layout_speed_view.xml +++ b/core/function-impl/mogo-core-function-hmi/src/main/res/layout/layout_speed_view.xml @@ -6,13 +6,14 @@ tools:layout_width="wrap_content" tools:layout_height="@dimen/dp_175"> - + tools:text="60" + tools:ignore="SpUsage" />