layout traffic light in vr mode
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package com.mogo.module.extensions.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Color;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.IntDef;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
|
||||
import com.mogo.module.extensions.R;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* vr模式下的纵向显示的红绿灯封装
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
public class VerticalTrafficLightView extends ConstraintLayout {
|
||||
private static final String TAG = "VerticalTrafficLightView";
|
||||
public static final int TRAFFIC_LIGHT_COLOR_GRAY = 0;
|
||||
public static final int TRAFFIC_LIGHT_COLOR_RED = 1;
|
||||
public static final int TRAFFIC_LIGHT_COLOR_YELLOW = 2;
|
||||
public static final int TRAFFIC_LIGHT_COLOR_GREEN = 3;
|
||||
|
||||
private ImageView ivTrafficLight, ivNoLeftTime;
|
||||
private TextView tvLeftTime, tvLeftTimeUnit;
|
||||
private Group groupLeftTime;
|
||||
|
||||
private static final int[] TURN_AROUND_ICON_RES = new int[]{R.drawable.module_ext_dw_traffic_turn_around_gray, R.drawable.module_ext_dw_traffic_turn_around_red, R.drawable.module_ext_dw_traffic_turn_around_yellow, R.drawable.module_ext_dw_traffic_turn_around_green};
|
||||
private static final int[] TURN_LEFT_ICON_RES = new int[]{R.drawable.module_ext_dw_traffic_turn_left_gray, R.drawable.module_ext_dw_traffic_turn_left_red, R.drawable.module_ext_dw_traffic_turn_left_yellow, R.drawable.module_ext_dw_traffic_turn_left_green};
|
||||
private static final int[] STRAIGHT_ICON_RES = new int[]{R.drawable.module_ext_dw_traffic_straight_gray, R.drawable.module_ext_dw_traffic_straight_red, R.drawable.module_ext_dw_traffic_straight_yellow, R.drawable.module_ext_dw_traffic_straight_green};
|
||||
private static final int[] TURN_RIGHT_ICON_RES = new int[]{R.drawable.module_ext_dw_traffic_turn_right_gray, R.drawable.module_ext_dw_traffic_turn_right_red, R.drawable.module_ext_dw_traffic_turn_right_yellow, R.drawable.module_ext_dw_traffic_turn_right_green};
|
||||
|
||||
private final int[] iconRes;
|
||||
private final int[] colorRes = new int[]{-1, Color.parseColor("#F63A35"), Color.parseColor("#F63A35"), Color.parseColor("#11FF89")};
|
||||
|
||||
public VerticalTrafficLightView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public VerticalTrafficLightView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public VerticalTrafficLightView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
LayoutInflater.from(context).inflate(R.layout.merge_vertical_traffic_light_in_vr, this);
|
||||
initView();
|
||||
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.VerticalTrafficLightView, 0, 0);
|
||||
int lightType = typedArray.getInt(R.styleable.VerticalTrafficLightView_iconRes, 0);
|
||||
typedArray.recycle();
|
||||
switch (lightType) {
|
||||
case 1:
|
||||
// turn left
|
||||
iconRes = TURN_LEFT_ICON_RES;
|
||||
break;
|
||||
case 2:
|
||||
// straight
|
||||
iconRes = STRAIGHT_ICON_RES;
|
||||
break;
|
||||
case 3:
|
||||
// turn right
|
||||
iconRes = TURN_RIGHT_ICON_RES;
|
||||
break;
|
||||
default:
|
||||
// turn around
|
||||
iconRes = TURN_AROUND_ICON_RES;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
ivTrafficLight = findViewById(R.id.module_ext_id_traffic_light_icon);
|
||||
ivNoLeftTime = findViewById(R.id.module_ext_id_traffic_light_no_left_time);
|
||||
tvLeftTime = findViewById(R.id.module_ext_id_traffic_light_left_time);
|
||||
tvLeftTimeUnit = findViewById(R.id.module_ext_id_traffic_light_left_time_unit);
|
||||
groupLeftTime = findViewById(R.id.module_ext_id_group_left_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置红绿灯的颜色,根据颜色来展示不同的效果
|
||||
*
|
||||
* @param color 红绿灯颜色{@link #TRAFFIC_LIGHT_COLOR_GRAY},{@link #TRAFFIC_LIGHT_COLOR_RED}等四个颜色
|
||||
*/
|
||||
private void setTrafficLightColor(@TrafficLightColor int color) {
|
||||
if (iconRes == null) {
|
||||
Logger.e(TAG, "红绿灯Icon数据为空,无法进行设置");
|
||||
return;
|
||||
}
|
||||
ivTrafficLight.setImageResource(iconRes[color]);
|
||||
if (color != TRAFFIC_LIGHT_COLOR_GRAY) {
|
||||
tvLeftTime.setTextColor(colorRes[color]);
|
||||
tvLeftTimeUnit.setTextColor(colorRes[color]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置红绿灯剩余时长
|
||||
*
|
||||
* @param leftTime 剩余时长,null或者empty表示没有时长数据
|
||||
*/
|
||||
private void setTrafficLightLeftTime(String leftTime) {
|
||||
if (leftTime == null || leftTime.isEmpty()) {
|
||||
groupLeftTime.setVisibility(View.GONE);
|
||||
ivNoLeftTime.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
groupLeftTime.setVisibility(View.VISIBLE);
|
||||
ivNoLeftTime.setVisibility(View.GONE);
|
||||
tvLeftTime.setText(leftTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置红绿灯状态,需设置颜色及时长
|
||||
*
|
||||
* @param color 红绿灯颜色,使用{@link #TRAFFIC_LIGHT_COLOR_RED}等四个值
|
||||
* @param leftTime 剩余时长,null或者empty表示没有时长数据
|
||||
*/
|
||||
public void setTrafficLightStatus(@TrafficLightColor int color, String leftTime) {
|
||||
setTrafficLightColor(color);
|
||||
setTrafficLightLeftTime(leftTime);
|
||||
}
|
||||
|
||||
@IntDef({TRAFFIC_LIGHT_COLOR_GRAY, TRAFFIC_LIGHT_COLOR_GREEN, TRAFFIC_LIGHT_COLOR_RED, TRAFFIC_LIGHT_COLOR_YELLOW})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface TrafficLightColor {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user