我可以访问默认缩放控件的放大和缩小按钮,如下所示:
View zoomControls = mapFragment.getView().findViewById(0x1);
for(int i=0;i<((ViewGroup)zoomControls).getChildCount();i++){
View child=((ViewGroup)zoomControls).getChildAt(i);
if (i==0) {
btn_zoomIn = child;
// there is your "+" button, zoom in
child.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
updateZoom = new ZoomInMapThread();
updateZoom.start();
break;
case MotionEvent.ACTION_UP:
updateZoom.interrupt();
zoomingIn++;
break;
}
return true;
}
});
}
if (i==1) {
btn_zoomOut = child;
// there is your "+" button, zoom in
child.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
updateZoom = new ZoomOutMapThread();
updateZoom.start();
break;
case MotionEvent.ACTION_UP:
updateZoom.interrupt();
zoomingOut++;
break;
}
return true;
}
});
}
}
我在此处启动 Threads 以通过在按下按钮时放大或缩小来更新 UI。我接下来要做的是在用户达到 maxZoom/minZoom 时更改按钮的外观。这里是放大/缩小线程的代码:
private class ZoomOutMapThread extends Thread {
@Override
public void run(){
try {
while (!Thread.currentThread().isInterrupted()){
runOnUiThread(new Runnable() {
@Override
public void run() {
currentZoomLevel = currentZoomLevel - 0.1f;
if (currentZoomLevel < minZoom){
currentZoomLevel = minZoom;
btn_zoomIn.setVisibility(View.VISIBLE);
btn_zoomOut.setVisibility(View.INVISIBLE);
} else {
btn_zoomIn.setVisibility(View.VISIBLE);
btn_zoomOut.setVisibility(View.VISIBLE);
}
mMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel));
Log.d("Zoom", "Level:" + currentZoomLevel);
}
});
Thread.sleep(100);
}
} catch (InterruptedException consumed) {
consumed.printStackTrace();
}
}
}
private class ZoomInMapThread extends Thread {
@Override
public void run(){
try {
while (!Thread.currentThread().isInterrupted()){
runOnUiThread(new Runnable() {
@Override
public void run() {
currentZoomLevel = currentZoomLevel + 0.1f;
if (currentZoomLevel > maxZoom){
currentZoomLevel = maxZoom;
btn_zoomIn.setVisibility(View.INVISIBLE);
btn_zoomOut.setVisibility(View.VISIBLE);
} else {
btn_zoomIn.setVisibility(View.VISIBLE);
btn_zoomOut.setVisibility(View.VISIBLE);
}
mMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel));
Log.d("Zoom","Level:"+currentZoomLevel);
}
});
Thread.sleep(100);
}
} catch (InterruptedException consumed) {
consumed.printStackTrace();
}
}
}
现在,我将它设置为不可见,但相反,我希望它看起来像一个禁用/不可点击的按钮。关于如何在像这里这样的一般视图上做到这一点的任何想法?