在Google地图上制作动画手势动画

时间:2018-01-18 00:03:20

标签: android google-maps android-layout google-maps-android-api-2

我在地图上有一个叠加层(ImageView),我可以在其上捕捉多个事件,例如滚动,双击或轻弹,然后在下面的地图上模拟这些事件。滚动和双击是有效的,但我不知道如何接近这一事件。这是我的代码:

@Override
public boolean onDoubleTap(MotionEvent e) {
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomIn(), DEFAULT_ZOOM_SPEED, null);
    return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    LatLng target = mGoogleMap.getCameraPosition().target;
    Point screenPoint = mGoogleMap.getProjection().toScreenLocation(target);
    Point newPoint = new Point(screenPoint.x + (int) distanceX, screenPoint.y + (int) distanceY);
    LatLng mapNewTarget = mGoogleMap.getProjection().fromScreenLocation(newPoint);

    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
                mapNewTarget, mGoogleMap.getCameraPosition().zoom);
    mGoogleMap.moveCamera(update);
    return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {   }

1 个答案:

答案 0 :(得分:0)

onDoubleTap()方法中的其他情况(onScroll()onFling())一样,您可以实现所需的一切,例如:

1)滚动(您需要将velocityXvelocityY转换为distanceXdistanceY以获取该内容:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    final float FLING_DURATION = 0.1f;
    float distanceX = - FLING_DURATION * velocityX;
    float distanceY = - FLING_DURATION * velocityY;

    LatLng target = mGoogleMap.getCameraPosition().target;
    Point screenPoint = mGoogleMap.getProjection().toScreenLocation(target);
    Point newPoint = new Point(screenPoint.x + (int) distanceX, screenPoint.y + (int) distanceY);
    LatLng mapNewTarget = mGoogleMap.getProjection().fromScreenLocation(newPoint);

    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
            mapNewTarget, mGoogleMap.getCameraPosition().zoom);
    mGoogleMap.animateCamera(update);

    return true;
}

(也可以调整FLING_DURATION)

2)改变地图倾斜度:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    float tilt = velocityY > 0 ? 90 : 0;
    LatLng target = mGoogleMap.getCameraPosition().target;
    CameraUpdate update = CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
            .target(target)
            .tilt(tilt)
            .zoom(mGoogleMap.getCameraPosition().zoom)
            .bearing(mGoogleMap.getCameraPosition().bearing)
            .build());
    mGoogleMap.animateCamera(update);

    return true;
}

3)或地图类型:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    int mapType = mGoogleMap.getMapType();
    if (velocityX > 0) {
        mapType++;
        if (mapType > GoogleMap.MAP_TYPE_HYBRID) {
            mapType = GoogleMap.MAP_TYPE_HYBRID;
        }
    } else {
        mapType--;
        if (mapType < GoogleMap.MAP_TYPE_NORMAL) {
            mapType = GoogleMap.MAP_TYPE_NORMAL;
        }
    }
    return true;
}

等等。

NB!并且不要忘记从true方法返回onDown(),否则其他任何手势都不起作用。