渐变色实现方式修改

This commit is contained in:
liujing
2022-06-01 17:58:51 +08:00
parent 969eb437eb
commit 6470a38fa7
2 changed files with 51 additions and 27 deletions

View File

@@ -2,12 +2,13 @@ package com.mogo.eagle.core.function.hmi.ui.widget
import android.animation.ValueAnimator
import android.content.Context
import android.content.res.Resources
import android.content.res.TypedArray
import android.graphics.*
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import android.view.animation.OvershootInterpolator
import androidx.core.content.ContextCompat
import com.mogo.eagle.core.function.hmi.R
@@ -32,11 +33,13 @@ class CircularProgressView @JvmOverloads constructor(
private var mRectF: RectF? = null
// 圆环渐变色
private var mColorArray: IntArray? = null
private var mConvertColorsArray: IntArray? = null
// 圆环进度(0-100) 初始化进度
private var mProgress: Int = typedArray.getInteger(R.styleable.CircularProgressView_progress, 0)
var dm: DisplayMetrics = resources.displayMetrics
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
@@ -61,9 +64,9 @@ class CircularProgressView @JvmOverloads constructor(
val startColor = typedArray.getColor(R.styleable.CircularProgressView_progStartColor, -1)
val firstColor = typedArray.getColor(R.styleable.CircularProgressView_progFirstColor, -1)
if (startColor != -1 && firstColor != -1) {
mColorArray = intArrayOf(startColor, firstColor)
mConvertColorsArray = intArrayOf(startColor, firstColor)
} else {
mColorArray = null
mConvertColorsArray = null
}
typedArray.recycle();
}
@@ -78,24 +81,43 @@ class CircularProgressView @JvmOverloads constructor(
val mRectT = getPaddingTop() + (viewHigh - mRectLength) / 2
mRectF = RectF(mRectL.toFloat(), mRectT.toFloat(), (mRectL + mRectLength).toFloat(),
(mRectT + mRectLength).toFloat())
// 设置进度圆环渐变色
mColorArray?.let {
mProgPaint.shader = LinearGradient(
0f, 0f, 0f,
measuredWidth.toFloat(), mColorArray!![0], mColorArray!![1], Shader.TileMode.MIRROR)
}
// // 设置进度圆环渐变色
// mColorArray?.let {
//// mProgPaint.shader = LinearGradient(
//// 0f, 0f, 0f,
//// measuredWidth.toFloat(), mColorArray!![0], mColorArray!![1], Shader.TileMode.MIRROR)
// sweepGradient()
// }
Log.d(TAG, mRectL.toString() + "," + mRectT.toString())
Log.d(TAG, mRectF.toString() + "," + "width:" + measuredWidth.toString() + "," + "PaddingLeft:" + getPaddingLeft().toString())
}
//渐变色
fun sweepGradient() {
if (mConvertColorsArray == null) {
return
}
val position = FloatArray(2)
position[0] = 0.0f;
position[1] = mProgress.toFloat() * 0.01f
Log.d(TAG, "mProgress" + mProgress.toString())
val sweepGradient = SweepGradient((measuredWidth / 2).toFloat(), (measuredHeight/ 2).toFloat(), mConvertColorsArray!!, position)
val matrix = Matrix()
matrix.setRotate(-90F, (measuredWidth / 2).toFloat(), (measuredHeight / 2).toFloat())
sweepGradient.setLocalMatrix(matrix);
mProgPaint.setShader(sweepGradient)
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
Log.d(TAG, "onDraw")
canvas?.let {
mRectF?.let { it1 -> it.drawArc(it1, 0.0f, 360.0f, false, mBackPaint) }
mRectF?.let { it1 ->
it.drawArc(it1, 275.0f,
(360 * mProgress / 100).toFloat(), false, mProgPaint)
sweepGradient()
var degree: Float = 3.6f * (mProgress.toFloat())
it.drawArc(it1, 275.0f, degree, false, mProgPaint)
}
}
@@ -136,11 +158,13 @@ class CircularProgressView @JvmOverloads constructor(
val animator = ValueAnimator.ofInt(mProgress, progress)
animator.addUpdateListener {
mProgress = it.animatedValue as Int
Log.d(TAG, "setProgress" + mProgress.toString())
invalidate()
}
animator.interpolator = OvershootInterpolator()
animator.interpolator = LinearInterpolator()
animator.duration = animTime
animator.start()
}
}
@@ -192,17 +216,17 @@ class CircularProgressView @JvmOverloads constructor(
}
fun setProgColor(startColor: Int, endColor: Int) {
mColorArray = intArrayOf(ContextCompat.getColor(context, startColor), ContextCompat.getColor(context, endColor))
mColorArray?.let {
if (mRectF != null) {
mProgPaint.shader = LinearGradient(0f, 0f, 0f,
getMeasuredWidth().toFloat(), mColorArray!![0], mColorArray!![1], Shader.TileMode.MIRROR)
}
}
Log.d(TAG, "setProgColor:" + getMeasuredWidth().toString())
mConvertColorsArray = intArrayOf(ContextCompat.getColor(context, startColor), ContextCompat.getColor(context, endColor))
// mColorArray?.let {
// if (mRectF != null) {
//// mProgPaint.shader = LinearGradient(0f, 0f, 0f,
//// getMeasuredWidth().toFloat(), mColorArray!![0], mColorArray!![1], Shader.TileMode.MIRROR)
// Log.d(TAG, "setProgColor:" + getMeasuredWidth().toString() + "mProgress:" + mProgress.toString())
// sweepGradient()
// }
// }
}
fun setProgColor(colorArray: IntArray) {
@@ -210,8 +234,8 @@ class CircularProgressView @JvmOverloads constructor(
if (it.size < 2) {
return
}
mColorArray = it.copyOf()
mColorArray?.let {
mConvertColorsArray = it.copyOf()
mConvertColorsArray?.let {
mProgPaint.shader = LinearGradient(0f, 0f, 0f,
getMeasuredWidth().toFloat(), it, null, Shader.TileMode.MIRROR)
}

View File

@@ -78,16 +78,16 @@ public class SteeringWheelView extends ConstraintLayout {
steeringCircularV = findViewById(R.id.steering_circular);
steeringCircularV.setBackWidth(8);
steeringCircularV.setBackColor(R.color.hmi_light_back_bg);
steeringCircularV.setProgress((int) (270 * 100) / 360, 2000);
steeringCircularV.setProgColor(R.color.hmi_light_blue, R.color.hmi_dark_blue);
steeringCircularV.setProgress((int) (180 * 100) / 360, 1000);
if (AppIdentityModeUtils.isTaxi(FunctionBuildConfig.appIdentityMode)) {
steeringCircularVAlpha = findViewById(R.id.steering_circular_alpha);
steeringCircularVAlpha.setProgress((int) (270 * 100) / 360, 2000);
steeringCircularV.setProgColor(R.color.hmi_dark_blue, R.color.hmi_light_blue);
steeringCircularVAlpha.setBackWidth(8);
steeringCircularVAlpha.setBackColor(R.color.hmi_clear_00);
steeringCircularVAlpha.setProgColor(R.color.hmi_light_blue_alpha_ff, R.color.hmi_light_blue_alpha_00);
steeringCircularVAlpha.setBlurMaskFilter(BlurMaskFilter.Blur.NORMAL, 12);
steeringCircularVAlpha.setProgress((int) (180 * 100) / 360, 1000);
}
}