這個是為了實現淘寶物流軌跡的那種樣式,軌跡路線是漸變色。
先說下物流軌跡實現流程。
1.高德地圖3d、導航、搜索三個sdk的支持
2.通過導航獲取一條路徑,這個路徑包含的點相當多,可能有上萬個。
3.利用路徑中的點划線,划線的點和導航提供的點是用的格式不一樣,需要簡單轉換一下。
4.使用android自帶的api “ArgbEvaluator”來計算每個點的顏色值。
5.把所有的顏色值放進一個數組,然后使用.colorValues(colorList).useGradient(true)這兩個設置,就可以把線繪制出來了。
@Override public void onCalculateRouteSuccess(int[] ints) { if(ints!=null&&ints.length>0){ AMapNaviPath naviPath = mAMapNavi.getNaviPath();//導航返回的路徑 if(naviPath.getCoordList()!=null){ ArgbEvaluator argbEvaluator = new ArgbEvaluator();//漸變色計算類 int colorStart = Color.parseColor("#FFA17A"); int colorEnd = Color.parseColor("#FF5934"); ArrayList<LatLng> pathPointList = new ArrayList<>(); List<Integer> colorList = new ArrayList<>(); int size = naviPath.getCoordList().size();//路徑上所有的點 for (int i = 0; i <size; i++) { NaviLatLng naviLatLng = naviPath.getCoordList().get(i); LatLng latLng = new LatLng(naviLatLng.getLatitude(),naviLatLng.getLongitude()); int currentColor = (int) (argbEvaluator.evaluate((float) i/(float)size, colorStart, colorEnd));//計算每個點需要的顏色值 colorList.add(currentColor); pathPointList.add(latLng); } PolylineOptions polylineOptions = new PolylineOptions() .width(DensityUtil.dip2px(4)) // .color(Color.parseColor("#FF5934")) .colorValues(colorList) .useGradient(true) .addAll(pathPointList); mMapControl.addPolyline(polylineOptions); } } }