高德地圖新增多邊形Polygon顏色異常的解決
阿新 • • 發佈:2019-01-31
高德地圖的覆蓋物新增API提供了很多種指定覆蓋物的新增和自定義覆蓋物新增的方法。標記、向量圖形元素(包括:折線、多邊形和圓)等。覆蓋物擁有自己的地理座標,當拖動或縮放地圖時,它們也會隨地圖移動。
在新增多邊形(Polygon)的時候,我發現並沒有設定Polygon區域內color的透明度的方法。
aMap.addPolygon(new PolygonOptions()
.addAll(createRectangle(Constants.SHANGHAI, 1, 1))
.fillColor(Color.LTGRAY).strokeColor (Color.RED).strokeWidth(1));
PolygonOptions options = new PolygonOptions();
int numPoints = 400;
float semiHorizontalAxis = 5f;
float semiVerticalAxis = 2.5f;
double phase = 2 * Math.PI / numPoints;
for (int i = 0; i <= numPoints; i++) {
options.add(new LatLng(Constants.BEIJING .latitude
+ semiVerticalAxis * Math.sin(i * phase),
Constants.BEIJING.longitude + semiHorizontalAxis
* Math.cos(i * phase)));
}
polygon = aMap.addPolygon(options.strokeWidth(4f)
.strokeColor(Color.BLACK).fillColor(Color.LTGRAY ));
其實Color本身就是帶透明度屬性的,只不過我們平常構造的時候都不這麼用,預設為255的alpha值,所以根本就是不透明。
使用Int color = Color.argb(127,255,0,255);Color中的靜態方法返回一個int的color值,這樣就解決了問題。
aMap.addPolygon(new PolygonOptions()
.addAll(createRectangle(Constants.SHANGHAI, 1, 1))
.fillColor(Color.LTGRAY).strokeColor(Color.argb(127,255,0,255)).strokeWidth(1));
PolygonOptions options = new PolygonOptions();
int numPoints = 400;
float semiHorizontalAxis = 5f;
float semiVerticalAxis = 2.5f;
double phase = 2 * Math.PI / numPoints;
for (int i = 0; i <= numPoints; i++) {
options.add(new LatLng(Constants.BEIJING.latitude
+ semiVerticalAxis * Math.sin(i * phase),
Constants.BEIJING.longitude + semiHorizontalAxis
* Math.cos(i * phase)));
}
polygon = aMap.addPolygon(options.strokeWidth(4f)
.strokeColor(Color.BLACK).fillColor(Color.LTGRAY));
這個問題確實比較簡單,是一個很小的知識點。
程式設計就是演算法和資料結構,演算法和資料結構是程式設計的靈魂。