[6.5.0]修复二轮灯态显示Bug

This commit is contained in:
xuxinchao
2024-07-19 15:33:21 +08:00
parent cc9f0e1ac1
commit 0e94b8bd65
3 changed files with 126 additions and 99 deletions

View File

@@ -126,22 +126,25 @@ class TrafficLightPromptView @JvmOverloads constructor(
nextState: TrafficLightEnum, nextDuration: Float,
nextTwoState: TrafficLightEnum, nextTwoDuration: Float) {
super.onShowTrafficLightPrompt(currentState, currentDuration,nextState, nextDuration, nextTwoState, nextTwoDuration)
if(currentDuration < 5){
//当前灯态倒计时时间小于5秒展示提示窗
updateTrafficLight(currentState, currentDuration,nextState)
if(currentLightState != currentState){
currentLightState = currentState
ThreadUtils.runOnUiThread {
if(this@TrafficLightPromptView.visibility == View.GONE && currentDuration < 1){
return@runOnUiThread
}
currentLightDuration = currentDuration
}else {
//当前灯态大于等于5秒且灯态发生变化此时应该将倒计时渐变为0之后关闭提示窗
if(currentState != currentLightState){
ThreadUtils.runOnUiThread{
mLightHandler.sendEmptyMessage(LIGHT_UPDATE)
if(currentDuration < 5){
//当前灯态倒计时时间小于5秒展示提示窗
updateTrafficLight(currentState, currentDuration,nextState)
if(currentLightState != currentState){
currentLightState = currentState
}
currentLightDuration = currentDuration
}else {
//当前灯态大于等于5秒且灯态发生变化此时应该将倒计时渐变为0之后关闭提示窗
if(currentState != currentLightState){
mLightHandler.sendEmptyMessage(LIGHT_UPDATE)
currentLightState = currentState
}else{
hideCurrentView()
}
currentLightState = currentState
}else{
hideCurrentView()
}
}
}

View File

@@ -35,9 +35,7 @@ class FusionTrafficLightView @JvmOverloads constructor(
private var user = 0 //使用方driver:0 passenger:1
private var mCurrentLightId = TrafficLightEnum.BLACK
private var totalDuration = 0 //一轮灯态的总时长
private var maxGreenDuration = 0 //绿灯最长时间
private var maxYellowDuration = 0 //黄灯最长时间
private var maxRedDuration = 0 //红灯最长时间
private var currentAngle = 0f //指针指向角度
private var fusionTrafficLightNum: TypefaceTextView ?=null //融合红绿灯倒计时
private var fusionTrafficLightState: ImageView ?= null //融合红绿灯灯态
@@ -48,6 +46,9 @@ class FusionTrafficLightView @JvmOverloads constructor(
private var currentLightState = TrafficLightEnum.BLACK //当前灯态
private var currentLightDuration = 0 //当前灯态倒计时
private var redLightRoundNum = 0 //当前路口红灯出现的轮数
private var greenLightRoundNum = 0 //当前路口绿灯出现的轮数
private var yellowLightRoundNum = 0 //当前路口黄灯出现的轮数
init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FusionTrafficLightView)
@@ -84,6 +85,46 @@ class FusionTrafficLightView @JvmOverloads constructor(
mCurrentLightId = TrafficLightEnum.BLACK
this@FusionTrafficLightView.visibility = GONE
CallerHmiViewControlListenerManager.invokeV2XEvent(View.GONE, TAG)
//将灯态轮归零
returnToZero()
}
}
/**
* 将灯态轮归零
*/
private fun returnToZero(){
redLightRoundNum = 0 //当前路口红灯出现的轮数
greenLightRoundNum = 0 //当前路口绿灯出现的轮数
yellowLightRoundNum = 0 //当前路口黄灯出现的轮数
}
/**
* 判断是否进入新的一轮灯态
* @param currentState 当前灯态
*/
private fun judgeRoundNun(currentState: TrafficLightEnum): Boolean{
if(currentLightState == currentState){
return false
}else{
currentLightState = currentState
when(currentState){
TrafficLightEnum.RED->{
redLightRoundNum++
return redLightRoundNum > greenLightRoundNum && redLightRoundNum > yellowLightRoundNum
}
TrafficLightEnum.GREEN->{
greenLightRoundNum++
return greenLightRoundNum > redLightRoundNum && greenLightRoundNum > yellowLightRoundNum
}
TrafficLightEnum.YELLOW->{
yellowLightRoundNum++
return yellowLightRoundNum > redLightRoundNum && yellowLightRoundNum > greenLightRoundNum
}
TrafficLightEnum.BLACK->{
return false
}
}
}
}
@@ -108,75 +149,46 @@ class FusionTrafficLightView @JvmOverloads constructor(
if(currentLightState == currentState && currentLightDuration == currentDuration){
return
}
currentLightState = currentState
currentLightDuration = currentDuration
ThreadUtils.runOnUiThread {
//如果初次获取的路口灯态倒计时时长小于1秒则返回不处理灯态进入下一轮灯态开始进行显示
if(this@FusionTrafficLightView.visibility == View.GONE && currentDuration < 1){
return@runOnUiThread
}
//如果当前红绿灯视图为隐藏状态则设置为显示状态
if(this@FusionTrafficLightView.visibility == View.GONE){
this@FusionTrafficLightView.visibility = View.VISIBLE
if(judgeRoundNun(currentState)){
if(this@FusionTrafficLightView.visibility == View.GONE){
this@FusionTrafficLightView.visibility = View.VISIBLE
}
CallerHmiViewControlListenerManager.invokeV2XEvent(View.VISIBLE, TAG)
AutopilotSummaryInfo.lightServicesNum++
//首次展示,需要将当前、下一、下二灯态的时间占比绘制出来,只绘制一次
val proportionList = intArrayOf(0, 0, 0)
//按照绿、黄、红的顺序将灯态时间添加到数组
when (currentState) {
TrafficLightEnum.GREEN -> {
proportionList[0] = currentDuration
maxGreenDuration = currentDuration
}
TrafficLightEnum.YELLOW -> {
maxYellowDuration = if(currentDuration < 3){
3
}else{
currentDuration
}
proportionList[1] = maxYellowDuration
}
else -> {
proportionList[2] = currentDuration
maxRedDuration = currentDuration
}
val colorList = ArrayList<TrafficLightEnum>()
val durationList = ArrayList<Int>()
colorList.add(currentState)
colorList.add(nextState)
colorList.add(nextTwoState)
if(currentDuration < 3){
durationList.add(3)
totalDuration = 3 + nextDuration + nextTwoDuration
currentAngle = 360f/totalDuration*(3-currentDuration)
}else{
durationList.add(currentDuration)
totalDuration = currentDuration + nextDuration + nextTwoDuration
currentAngle = 0f
}
when(nextState){
TrafficLightEnum.GREEN -> {
proportionList[0] = nextDuration
maxGreenDuration = nextDuration
}
TrafficLightEnum.YELLOW -> {
maxYellowDuration = if(nextDuration<3){
3
}else{
nextDuration
}
proportionList[1] = maxYellowDuration
}
else -> {
proportionList[2] = nextDuration
maxRedDuration = nextDuration
}
durationList.add(nextDuration)
durationList.add(nextTwoDuration)
//没轮灯态绘制一次灯态时长比例
fusionTrafficLightProportion?.updateProportion(durationList,colorList)
}else{
if(currentAngle < 360f){
currentAngle += 360f/totalDuration
}
when(nextTwoState){
TrafficLightEnum.GREEN -> {
proportionList[0] = nextTwoDuration
maxGreenDuration = nextTwoDuration
}
TrafficLightEnum.YELLOW -> {
maxYellowDuration = if(nextTwoDuration<3){
3
}else{
nextTwoDuration
}
proportionList[1] = maxYellowDuration
}
else -> {
proportionList[2] = nextTwoDuration
maxRedDuration = nextTwoDuration
}
}
fusionTrafficLightProportion?.updateProportion(proportionList.asList())
totalDuration = maxGreenDuration + maxYellowDuration + maxRedDuration
}
//根据当前灯态设置转盘、刻度、指针背景
when(currentState){
TrafficLightEnum.GREEN -> {
@@ -214,19 +226,6 @@ class FusionTrafficLightView @JvmOverloads constructor(
//更新指针指向
val pointerLayoutParams = fusionTrafficLightPointer?.layoutParams as LayoutParams
val currentAngle = when (currentState) {
TrafficLightEnum.GREEN -> {
360f*(maxGreenDuration-currentDuration)/totalDuration
}
TrafficLightEnum.YELLOW -> {
360f*(maxGreenDuration+maxYellowDuration-currentDuration)/totalDuration
}
else -> {
360f*(maxGreenDuration+maxYellowDuration+maxRedDuration-currentDuration)/totalDuration
}
}
pointerLayoutParams.circleAngle = currentAngle
fusionTrafficLightPointer?.rotation = currentAngle
fusionTrafficLightPointer?.layoutParams = pointerLayoutParams

View File

@@ -11,6 +11,8 @@ import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import com.mogo.eagle.core.data.enums.TrafficLightEnum;
import com.mogo.eagle.core.function.hmi.R;
import java.util.ArrayList;
@@ -30,6 +32,9 @@ public class ProportionChartView extends View {
private RectF mRectF;
//红绿灯绿灯、黄灯、红灯灯色时间
private List<Integer> proportionList = new ArrayList<>();
//红绿灯灯态
private List<TrafficLightEnum> lightStatusList = new ArrayList<>();
//一轮灯态的总时长
private int totalDuration = 0;
@@ -52,15 +57,17 @@ public class ProportionChartView extends View {
/**
* 更新红绿灯时间
* @param list 时间列表
* @param durationList 时间列表
* @param colorList 灯态列表
*/
public void updateProportion(List<Integer> list){
if(list != null){
this.proportionList = list;
public void updateProportion(List<Integer> durationList, List<TrafficLightEnum> colorList){
if(durationList != null && colorList != null){
this.proportionList = durationList;
totalDuration = 0;
for(int element: proportionList){
totalDuration += element;
}
this.lightStatusList = colorList;
postInvalidate();
}
}
@@ -148,26 +155,44 @@ public class ProportionChartView extends View {
sweepAngle = 360f*proportionList.get(i)/totalDuration;
SweepGradient sweepGradient;
//设置渐变颜色
if(i == 0){
if(lightStatusList.get(i) == TrafficLightEnum.GREEN){
//设置绿灯渐变色
sweepGradient = new SweepGradient(getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f,
greenColorArray,null);
Matrix rotateMatrix = new Matrix();
rotateMatrix.setRotate(270,getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
if(i == 0){
rotateMatrix.setRotate(270,getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}else if(i == 1){
rotateMatrix.setRotate(270+(360*proportionList.get(0)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}else{
rotateMatrix.setRotate(270+(360*proportionList.get(0)+proportionList.get(1)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}
sweepGradient.setLocalMatrix(rotateMatrix);
}else if(i == 1){
}else if(lightStatusList.get(i) == TrafficLightEnum.YELLOW){
//设置黄灯渐变色
sweepGradient = new SweepGradient(getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f,
yellowColorArray,null);
Matrix rotateMatrix = new Matrix();
rotateMatrix.setRotate(270+(360*proportionList.get(0)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
if(i == 0){
rotateMatrix.setRotate(270,getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}else if(i == 1){
rotateMatrix.setRotate(270+(360*proportionList.get(0)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}else{
rotateMatrix.setRotate(270+(360*proportionList.get(0)+proportionList.get(1)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}
sweepGradient.setLocalMatrix(rotateMatrix);
}else{
//设置红灯渐变色
sweepGradient = new SweepGradient(getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f,
redColorArray,null);
Matrix rotateMatrix = new Matrix();
rotateMatrix.setRotate(270+(360*proportionList.get(0)+proportionList.get(1)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
if(i == 0){
rotateMatrix.setRotate(270,getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}else if(i == 1){
rotateMatrix.setRotate(270+(360*proportionList.get(0)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}else{
rotateMatrix.setRotate(270+(360*proportionList.get(0)+proportionList.get(1)),getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
}
sweepGradient.setLocalMatrix(rotateMatrix);
}
mRingPaint.setShader(sweepGradient);