完成最优路线的颜色梯度计算

增加逻辑,3D模式下关闭道路事件连线的绘制
This commit is contained in:
董宏宇
2021-04-16 17:53:54 +08:00
parent af05b71bdd
commit a2ef2bbd44
4 changed files with 69 additions and 44 deletions

1
.idea/gradle.xml generated
View File

@@ -91,6 +91,7 @@
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
<option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings>
</option>
</component>

View File

@@ -1,5 +1,7 @@
package com.mogo.utils;
import android.graphics.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
@@ -12,60 +14,73 @@ import java.util.regex.Pattern;
public class ColorUtils {
/**
* @return 获取渐变色集合
* ARGB颜色 转 HEX颜色
*/
public static List<Integer> gradient(String startColor, String endColor, int step) {
// 将HEX转为RGB
int[] sColor = hexToRgb(startColor);
int[] eColor = hexToRgb(endColor);
// 计算每一步的差值
int rStep = (eColor[0] - sColor[0]) / step;
int gStep = (eColor[1] - sColor[1]) / step;
int bStep = (eColor[2] - sColor[2]) / step;
// 生成渐变色
List<Integer> gradientColorArr = new ArrayList<>();
for (int i = 0; i < step; i++) {
public static String argbToHex(int alpha, int red, int green, int blue) {
if (alpha < 0 || alpha > 255
|| red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
return "";
}
return gradientColorArr;
String alphaStr = Integer.toHexString(alpha);
String redStr = Integer.toHexString(red);
String greenStr = Integer.toHexString(green);
String blueStr = Integer.toHexString(blue);
return ("#" + alphaStr + redStr + greenStr + blueStr).toUpperCase();
}
/**
* HEX颜色 转 RGB颜色
* HEX颜色 转 ARGB颜色
*/
public static int[] hexToRgb(String hex) {
int[] rgb = new int[3];
if (!Pattern.matches("^#[0-9a-f[A-F]]{6}", hex)) {
public static int[] hexToArgb(String hex) {
int[] rgb = new int[4];
if (!Pattern.matches("^#[0-9a-f[A-F]]{8}", hex)) {
return rgb;
}
String redStr = hex.substring(1, 3);
String greenStr = hex.substring(3, 5);
String blueStr = hex.substring(5);
String alphaStr = hex.substring(1, 3);
String redStr = hex.substring(3, 5);
String greenStr = hex.substring(5, 7);
String blueStr = hex.substring(7);
rgb[0] = Integer.valueOf(redStr, 16);
rgb[1] = Integer.valueOf(greenStr, 16);
rgb[2] = Integer.valueOf(blueStr, 16);
rgb[0] = Integer.valueOf(alphaStr, 16);
rgb[1] = Integer.valueOf(redStr, 16);
rgb[2] = Integer.valueOf(greenStr, 16);
rgb[3] = Integer.valueOf(blueStr, 16);
return rgb;
}
/**
* RGB颜色 转 HEX颜色
* 对传入颜色生成梯度透明的颜色集合
*
* @param startColor 开始颜色
* @param startColor 结束颜色
* @param step 步长
* @return 生成的梯度颜色集合
*/
public static String rgbToHex(int red, int green, int blue) {
if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0
|| blue > 255) {
return "";
public static List<Integer> gradientAlpha(String startColor, String endColor, int step) {
// 将HEX转为RGB
int[] sColor = hexToArgb(startColor);
int[] eColor = hexToArgb(endColor);
// 计算每一步的差值
int aStep = (eColor[0] - sColor[0]) / step;
int rStep = (eColor[1] - sColor[1]) / step;
int gStep = (eColor[2] - sColor[2]) / step;
int bStep = (eColor[3] - sColor[3]) / step;
// 生成渐变色
List<Integer> gradientColorArr = new ArrayList<>();
for (int i = 0; i < step; i++) {
gradientColorArr.add(
Color.argb(aStep * i + sColor[0],
rStep * i + sColor[1],
gStep * i + sColor[2],
bStep * i + sColor[3]));
}
String redStr = Integer.toHexString(red);
String greenStr = Integer.toHexString(green);
String blueStr = Integer.toHexString(blue);
return ("#" + redStr + greenStr + blueStr).toUpperCase();
return gradientColorArr;
}
}

View File

@@ -35,7 +35,9 @@ public class MoGoV2XPolylineManager implements IMoGoV2XPolylineManager {
}
if ((V2XServiceManager.getMoGoV2XStatusManager().isRoadEventPOIShow()
|| V2XServiceManager.getMoGoV2XStatusManager().isOtherSeekHelpPOIShow())
&& V2XServiceManager.getV2XStatusManager().getTargetMoGoLatLng() != null) {
&& V2XServiceManager.getV2XStatusManager().getTargetMoGoLatLng() != null
&& !V2XServiceManager.getMoGoStatusManager().isVrMode()
) {
// 连接线参数
MogoPolylineOptions options = new MogoPolylineOptions();

View File

@@ -6,6 +6,7 @@ import com.mogo.map.overlay.IMogoPolyline;
import com.mogo.map.overlay.MogoPolylineOptions;
import com.mogo.module.v2x.V2XServiceManager;
import com.mogo.module.v2x.utils.LocationUtils;
import com.mogo.utils.ColorUtils;
import java.util.ArrayList;
import java.util.List;
@@ -22,16 +23,15 @@ public class V2XOptimalRouteOverlay {
private MogoPolylineOptions mPolylineOptions;
// 线路径集合
private List<MogoLatLng> mPolylinePointList;
// 渐变色
private List<Integer> mPolylineColors;
public V2XOptimalRouteOverlay() {
mPolylineOptions = new MogoPolylineOptions();
// 渐变色
List<Integer> colors = new ArrayList<>();
colors.add(0xFFF95959);
// 线条粗细,渐变,渐变色值
mPolylineOptions.width(25).useGradient(true).colorValues(colors);
// 绘制路径集合
mPolylinePointList = new ArrayList<>();
// 引导线颜色
mPolylineColors = new ArrayList<>();
}
/**
@@ -43,8 +43,10 @@ public class V2XOptimalRouteOverlay {
if (mMoGoPolyline != null) {
mMoGoPolyline.remove();
mPolylinePointList.clear();
mPolylineColors.clear();
}
if (polylinePoint != null) {
// 将当前车辆位置放进去
mPolylinePointList.add(new MogoLatLng(carLocal.getLatitude(), carLocal.getLongitude()));
// 过滤后台推送的推荐路线集合
@@ -54,8 +56,13 @@ public class V2XOptimalRouteOverlay {
mPolylinePointList.add(polyline);
}
}
mPolylineColors.addAll(ColorUtils.gradientAlpha("#002965ED", "#FF2965ED", mPolylinePointList.size()/2));
// 替换路径集合
mPolylineOptions.points(mPolylinePointList);
// 线条粗细,渐变,渐变色值
mPolylineOptions.width(25).useGradient(true).colorValues(mPolylineColors);
// 绘制线
mMoGoPolyline = V2XServiceManager.getMogoOverlayManager().addPolyline(mPolylineOptions);
}