我正在使用地图应用Android,并尝试在Google Maps v2 Android上使用AlphaSeekBar
到我的多边形。
我尝试使用SeekBar
处理我的所有多边形,但事实证明只是在最后一个多边形上工作。
这是我的代码:
private static final int ALPHA_MAX = 255;
private SeekBar mAlphaBar;
private Polygon mMutablePolygon;
private GoogleMap mMap;
.........................................
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kabupaten);
mAlphaBar = (SeekBar) findViewById(R.id.alphaSeekBar);
mAlphaBar.setMax(ALPHA_MAX);
mAlphaBar.setProgress(127);
mMessageView = (TextView) findViewById(R.id.message_text);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setPadding(currentLeft, currentTop, currentRight, currentBottom);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(DR, 14));
// poligon area --
{
PolygonOptions options = new PolygonOptions()
.add(dr_1,dr_2,dr_3,dr_4,dr_5,dr_6,dr_7,dr_8,dr_9,dr_10,dr_11,dr_12,dr_13,dr_14,dr_15,dr_16,dr_17) ;
mMutablePolygon = mMap.addPolygon(options
.strokeColor(Color.RED)
.strokeWidth(1)
.fillColor(Color.CYAN));
PolygonOptions gt =new PolygonOptions()
.add(gt_1,gt_2,gt_3,gt_4,gt_5,gt_6,gt_7,gt_8,gt_9,gt_10,gt_11,gt_12,gt_13,gt_14,gt_15,gt_16) ;
mMutablePolygon = mMap.addPolygon(gt
.strokeColor(Color.RED)
.strokeWidth(1)
.fillColor(Color.GREEN));
mAlphaBar.setOnSeekBarChangeListener(this);
}
// Add a camera change listener.
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition pos) {
mMessageView.setText("CameraChangeListener: " + pos);
}
});
}
}
}
public void onStopTrackingTouch(SeekBar seekBar) {
// Don't do anything here.
}
public void onStartTrackingTouch(SeekBar seekBar) {
// Don't do anything here.
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mMutablePolygon == null) {
return;
}
if (seekBar == mAlphaBar) {
int prevColor = mMutablePolygon.getFillColor();
mMutablePolygon.setFillColor(Color.argb(
progress, Color.red(prevColor), Color.green(prevColor),
Color.blue(prevColor)));
}
}
}
答案 0 :(得分:0)
问题是你只保留一个多边形的引用 如果要更新所有这些,则需要使用List mMutablePolygons;
然后创建每个多边形时:
mPolygon = mMap.addPolygon(options
.strokeColor(Color.RED)
.strokeWidth(1)
.fillColor(Color.CYAN));
mMutablePolygons.add(mPolygon);
并在更改搜索条值时:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mMutablePolygons == null || mMutablePolygons.isEmpty()) {
return;
}
if (seekBar == mAlphaBar) {
for(Polygon mpoly : mMutablePolygons){
int prevColor = mpoly.getFillColor();
mpoly.setFillColor(Color.argb(progress, Color.red(prevColor), Color.green(prevColor),Color.blue(prevColor)));
}
}
}