[6.5.0]融合红绿灯
This commit is contained in:
@@ -13,6 +13,7 @@ import com.mogo.eagle.core.function.api.datacenter.union.IMoGoTrafficLightListen
|
||||
import com.mogo.eagle.core.function.call.v2x.CallerTrafficLightListenerManager
|
||||
import com.mogo.eagle.core.function.hmi.R
|
||||
import com.mogo.eagle.core.utilcode.util.UiThreadHandler
|
||||
import kotlinx.android.synthetic.main.hmi_view_fusion_traffic_light.view.fusionTrafficLightBg
|
||||
import kotlinx.android.synthetic.main.hmi_view_fusion_traffic_light.view.fusionTrafficLightNum
|
||||
import kotlinx.android.synthetic.main.hmi_view_fusion_traffic_light.view.fusionTrafficLightPointer
|
||||
import kotlinx.android.synthetic.main.hmi_view_fusion_traffic_light.view.fusionTrafficLightProportion
|
||||
@@ -33,12 +34,29 @@ class FusionTrafficLightView @JvmOverloads constructor(
|
||||
private const val TAG = "FusionTrafficLightView"
|
||||
}
|
||||
|
||||
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 //红灯最长时间
|
||||
|
||||
|
||||
init {
|
||||
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FusionTrafficLightView)
|
||||
user = typedArray.getInt(R.styleable.FusionTrafficLightView_fusionLightUser,0)
|
||||
typedArray.recycle()
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
LayoutInflater.from(context).inflate(R.layout.hmi_view_fusion_traffic_light, this, true)
|
||||
CallerTrafficLightListenerManager.addListener(TAG, this)
|
||||
if(user == 0){
|
||||
fusionTrafficLightBg.setBackgroundResource(R.drawable.bg_fusion_traffic_light)
|
||||
}else{
|
||||
fusionTrafficLightBg.setBackgroundResource(R.drawable.bg_fusion_traffic_light_p)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
@@ -85,37 +103,51 @@ class FusionTrafficLightView @JvmOverloads constructor(
|
||||
when (currentState) {
|
||||
TrafficLightEnum.GREEN -> {
|
||||
proportionList.add(0,currentDuration)
|
||||
maxGreenDuration = currentDuration
|
||||
}
|
||||
TrafficLightEnum.YELLOW -> {
|
||||
proportionList.add(1,currentDuration)
|
||||
maxYellowDuration = currentDuration
|
||||
}
|
||||
else -> {
|
||||
proportionList.add(2,currentDuration)
|
||||
maxRedDuration = currentDuration
|
||||
}
|
||||
}
|
||||
when(nextState){
|
||||
TrafficLightEnum.GREEN -> {
|
||||
proportionList.add(0,nextDuration)
|
||||
maxGreenDuration = nextDuration
|
||||
}
|
||||
TrafficLightEnum.YELLOW -> {
|
||||
proportionList.add(1,nextDuration)
|
||||
maxYellowDuration = nextDuration
|
||||
}
|
||||
else -> {
|
||||
proportionList.add(2,nextDuration)
|
||||
maxRedDuration = nextDuration
|
||||
}
|
||||
}
|
||||
when(nextTwoState){
|
||||
TrafficLightEnum.GREEN -> {
|
||||
proportionList.add(0,nextTwoDuration)
|
||||
maxGreenDuration = nextTwoDuration
|
||||
}
|
||||
TrafficLightEnum.YELLOW -> {
|
||||
proportionList.add(1,nextTwoDuration)
|
||||
maxYellowDuration = nextTwoDuration
|
||||
}
|
||||
else -> {
|
||||
proportionList.add(2,nextTwoDuration)
|
||||
maxRedDuration = nextTwoDuration
|
||||
}
|
||||
}
|
||||
fusionTrafficLightProportion.updateProportion(proportionList)
|
||||
totalDuration = maxGreenDuration + maxYellowDuration + maxRedDuration
|
||||
Log.i("xuxinchao","totalDuration="+totalDuration)
|
||||
Log.i("xuxinchao","maxGreenDuration="+maxGreenDuration)
|
||||
Log.i("xuxinchao","maxYellowDuration="+maxYellowDuration)
|
||||
Log.i("xuxinchao","maxRedDuration="+maxRedDuration)
|
||||
}
|
||||
//根据当前灯态设置转盘、刻度、指针背景
|
||||
when(currentState){
|
||||
@@ -141,7 +173,37 @@ class FusionTrafficLightView @JvmOverloads constructor(
|
||||
}else{
|
||||
fusionTrafficLightNum.text = "0"
|
||||
}
|
||||
//TODO 更新指针指向
|
||||
//当时间为1开头时时间视觉上看不是左右居中对齐,需要做便宜操作
|
||||
if(currentDuration.toString().startsWith("1")){
|
||||
val numLayoutParams = fusionTrafficLightNum.layoutParams as LayoutParams
|
||||
numLayoutParams.rightMargin = 10
|
||||
fusionTrafficLightNum.layoutParams = numLayoutParams
|
||||
}else{
|
||||
val numLayoutParams = fusionTrafficLightNum.layoutParams as LayoutParams
|
||||
numLayoutParams.rightMargin = 0
|
||||
fusionTrafficLightNum.layoutParams = numLayoutParams
|
||||
}
|
||||
|
||||
//更新指针指向
|
||||
val pointerLayoutParams = fusionTrafficLightPointer.layoutParams as LayoutParams
|
||||
var currentAngle = 0f
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.SweepGradient;
|
||||
@@ -155,14 +156,23 @@ public class ProportionChartView extends View {
|
||||
//设置绿灯渐变色
|
||||
sweepGradient = new SweepGradient(getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f,
|
||||
greenColorArray,null);
|
||||
Matrix rotateMatrix = new Matrix();
|
||||
rotateMatrix.setRotate(270,getWidth()/2f,(getPaddingTop() + getHeight() - getPaddingBottom())/2f);
|
||||
sweepGradient.setLocalMatrix(rotateMatrix);
|
||||
}else if(i == 1){
|
||||
//设置黄灯渐变色
|
||||
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);
|
||||
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);
|
||||
sweepGradient.setLocalMatrix(rotateMatrix);
|
||||
}
|
||||
mRingPaint.setShader(sweepGradient);
|
||||
//画圆环
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
@@ -122,4 +122,11 @@
|
||||
<attr name="ringWidth" format="reference|dimension"/>
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="FusionTrafficLightView">
|
||||
<attr name="fusionLightUser">
|
||||
<enum name="driver" value="0"/>
|
||||
<enum name="passenger" value="1"/>
|
||||
</attr>
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
Reference in New Issue
Block a user