4

我正在根据设备的百分比LatLngBounds使用填充集将相机设置为动画,以便它可以在小型设备上运行。width

这甚至适用于具有 4 英寸显示屏的小型设备,但是它在 Android 7.0 中的多窗口模式和之前支持多窗口模式的设备中失败,例如。银河 S7。

在多窗口模式下的设备上出现以下异常:

Fatal Exception: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int, int, int): View size is too small after padding is applied.

这是可疑代码:

private void animateCamera() {

    // ...

    // Create bounds from positions
    LatLngBounds bounds = latLngBounds(positions);

    // Setup camera movement
    final int width = getResources().getDisplayMetrics().widthPixels;
    final int height = getResources().getDisplayMetrics().heightPixels;
    final int padding = (int) (width * 0.40); // offset from edges of the map in pixels
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);

    mMap.animateCamera(cu);
}

如何正确设置填充newLatLngBounds以在所有设备宽度和多窗口模式下工作?

4

1 回答 1

8

解决方案是在宽度和高度之间选择最小度量,因为在多窗口模式下,高度可以小于宽度:

private void animateCamera() {

    // ...

    // Create bounds from positions
    LatLngBounds bounds = latLngBounds(positions);

    // Setup camera movement
    final int width = getResources().getDisplayMetrics().widthPixels;
    final int height = getResources().getDisplayMetrics().heightPixels;
    final int minMetric = Math.min(width, height);
    final int padding = (int) (minMetric * 0.40); // offset from edges of the map in pixels
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);

    mMap.animateCamera(cu);
}
于 2016-10-24T19:22:18.830 回答