[shuttle_p_m2]
[turnlight]
This commit is contained in:
yangyakun
2023-12-06 16:45:07 +08:00
parent 420cecaf93
commit 39863abe1e
3 changed files with 186 additions and 3 deletions

View File

@@ -1,16 +1,199 @@
package com.mogo.och.bus.passenger.ui.widget
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import com.mogo.eagle.core.function.hmi.ui.vehicle.TurnLightViewStatus
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.mogo.eagle.core.function.api.datacenter.union.IMoGoTurnLightListener
import com.mogo.eagle.core.function.call.autopilot.CallerChassisLamplightListenerManager
import com.mogo.eagle.core.function.call.v2x.CallerTurnLightListenerManager
import com.mogo.eagle.core.utilcode.util.ThreadUtils
import com.mogo.och.bus.passenger.R
import com.mogo.och.common.module.manager.lightmanager.TurnLightManager
import kotlinx.android.synthetic.main.shuttle_p_jl_turn_light_status.view.left_nor_image
import kotlinx.android.synthetic.main.shuttle_p_jl_turn_light_status.view.left_select_image
import kotlinx.android.synthetic.main.shuttle_p_jl_turn_light_status.view.right_nor_image
import kotlinx.android.synthetic.main.shuttle_p_jl_turn_light_status.view.right_select_image
import kotlinx.android.synthetic.main.shuttle_p_jl_turn_light_status.view.turn_light_layout
/**
* @author: wangmingjun
* @date: 2023/2/13
*/
class M2TurnLightView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : TurnLightViewStatus(context, attrs) {
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr),
IMoGoTurnLightListener, TurnLightManager.TurnLightListener {
companion object {
private const val TAG = "TurnLightViewStatus"
}
private var isLeftLight: Boolean = false
private var isRightLight: Boolean = false
private var isDisappear: Boolean = false
init {
LayoutInflater.from(context)
.inflate(R.layout.shuttle_p_m2_turn_light_status, this, true)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
CallerTurnLightListenerManager.addListener(TAG,this)
TurnLightManager.addTurnLightStatusChangeListener(TAG,this)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
CallerChassisLamplightListenerManager.removeListener(TAG)
CallerTurnLightListenerManager.removeListener(TAG)
}
override fun hideTurnLightView() {
ThreadUtils.runOnUiThread{
if (!isDisappear) {
isDisappear = true
isLeftLight = false
isRightLight = false
animationDisappear()
}
}
}
override fun statusChange(newStatus: TurnLightManager.TurnLightStatus) {
ThreadUtils.runOnUiThread {
setTurnLight(newStatus)
}
}
/**
* 转向灯动画
*/
private fun setTurnLight(directionLight: TurnLightManager.TurnLightStatus) {
if (!isAttachedToWindow) {
return
}
//根据左右进行显示和隐藏,实际要判断每个来的时间和频度
when (directionLight) {
TurnLightManager.TurnLightStatus.TURN_LIGHT_LEFT -> { //左转向
if (!isLeftLight) {
isLeftLight = true
isRightLight = false
isDisappear = false
showNormalAnimation()
left_select_image.visibility = View.VISIBLE
right_select_image.visibility = View.GONE
right_select_image.clearAnimation()
setAnimation(left_select_image)
}
}
TurnLightManager.TurnLightStatus.TURN_LIGHT_RIGHT -> { //右转向
if (!isRightLight) {
isRightLight = true
isLeftLight = false
isDisappear = false
showNormalAnimation()
left_select_image.visibility = View.GONE
right_select_image.visibility = View.VISIBLE
left_select_image.clearAnimation()
setAnimation(right_select_image)
}
}
TurnLightManager.TurnLightStatus.TURN_LIGHT_NONE -> { //消失
if (!isDisappear) {
isDisappear = true
isLeftLight = false
isRightLight = false
animationDisappear()
}
}
}
}
//显示背景
private fun showNormalAnimation() {
val appearAnimation = AlphaAnimation(0f, 1.0f)
appearAnimation.duration = 300
val appearAnimationImage = AlphaAnimation(0f, 1.0f)
appearAnimation.duration = 500
turn_light_layout.startAnimation(appearAnimation)
left_nor_image.startAnimation(appearAnimationImage)
right_nor_image.startAnimation(appearAnimationImage)
turn_light_layout.visibility = View.VISIBLE
left_nor_image.visibility = View.VISIBLE
right_nor_image.visibility = View.VISIBLE
}
//消失动画,当转向等数据为空时候
private fun animationDisappear() {
left_select_image.visibility = View.GONE
right_select_image.visibility = View.GONE
left_select_image.clearAnimation()
right_select_image.clearAnimation()
left_nor_image.clearAnimation()
right_nor_image.clearAnimation()
turn_light_layout.clearAnimation()
val disappearAnimationLeft = AlphaAnimation(1.0f, 0f)
disappearAnimationLeft.duration = 300
val disappearAnimationBg = AlphaAnimation(1.0f, 0f)
disappearAnimationBg.duration = 500
left_nor_image.startAnimation(disappearAnimationLeft)
right_nor_image.startAnimation(disappearAnimationLeft)
turn_light_layout.startAnimation(disappearAnimationBg)
disappearAnimationLeft.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {
}
override fun onAnimationStart(p0: Animation?) {
}
override fun onAnimationEnd(p0: Animation?) {
left_nor_image.visibility = View.GONE
right_nor_image.visibility = View.GONE
}
})
disappearAnimationBg.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {
}
override fun onAnimationStart(p0: Animation?) {
}
override fun onAnimationEnd(p0: Animation?) {
turn_light_layout.visibility = View.GONE
}
})
}
//实现图片闪烁效果
private fun setAnimation(imageView: ImageView) {
val animationSet = AnimatorSet()
val valueAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1.0f)
val valueAnimatorDisappear = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0f)
valueAnimator.duration = 1000
valueAnimatorDisappear.duration = 800
valueAnimator.repeatCount = -1
valueAnimatorDisappear.repeatCount = -1
animationSet.playTogether(valueAnimatorDisappear, valueAnimator)
animationSet.start()
}
}