【问题标题】:Animations for the group of markers to rotate at the same time in google maps v2 android在谷歌地图v2 android中同时旋转标记组的动画
【发布时间】:2016-09-29 12:06:48
【问题描述】:

我正在 google map v2 中实现汽车组(标记)同时旋转的动画。

所以我需要在 handler.post() 方法中编写我的动画部分。

通过这样做,动画部分(handler.post() 方法)仅针对一个标记运行。

我在 for 循环中编写了 handler.post() 方法。它仅第一次运行,这意味着只有一个标记在旋转。之后它不起作用。我的代码如下。

private void animateCarsInMarkers(final MarkerOptions mark, final long bearing, final LatLng startPosition, final int position){

    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final long duration = 3000;
    final Interpolator interpolator = new LinearInterpolator();
    final Marker marker = mGoogleMap.addMarker(mark);

    final float rotationValue = Float.parseFloat(String.valueOf(bearing));

    try {
        if(tempCarsArray != null && !tempCarsArray.isEmpty()){
            sLongitude = tempCarsArray.get(position).getDouble(LONGITUDE);
            sLatitude = tempCarsArray.get(position).getDouble(LATITUDE);
            sBearing = tempCarsArray.get(position).getLong("Bearing");

            final double dLongitude = startPosition.longitude;
            final double dLatitude = startPosition.latitude;
            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float time = interpolator.getInterpolation((float) elapsed / duration);
                    double lng = time * dLongitude + (1 - time) * sLongitude;
                    double lat = time * dLatitude + (1 - time) * sLatitude;
                    float rotationValue = time *  dbearing + (1-time) * sBearing;
                    marker.setRotation((-rotationValue > 180) ? (rotationValue / 2) : rotationValue);
                    marker.setPosition(new LatLng(lat, lng));
                    if (time < 1.0) {
                        handler.postDelayed(this, 16);
                    }
                }
            });
        tempCarsArray.clear();
    } else {

            marker.setPosition(startPosition);
            marker.setRotation(-rotationValue > 180 ? rotationValue / 2 : rotationValue);
        }
    }catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我在 for 循环中调用的方法。但它只在第一次循环中运行。后来它不起作用。所以在这组标记中只有一个标记是动画的。我的for循环如下:

for(int i=0; i<size; i++){
animateCarsInMarkers(mark, bearing, latLng, i);
}

此循环仅在 i=0 的值时运行,并且不会再次运行。 提前致谢。

【问题讨论】:

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


    【解决方案1】:

    如果您的'for loop' 只运行了一次,则意味着您的“for 循环条件部分”,在本例中为i&lt;size,已经满足条件。我建议您实际记录“大小”并检查其值。

    如果您正在寻找 the size of an array,请使用 tempCarsArray.length,例如:

    for(int i=0; i < tempCarsArray.length; i++){
        animateCarsInMarkers(mark, bearing, latLng, i);
    }
    

    要检查是否是 size 引起了问题,试试这个。

    如果您知道要旋转的标记的实际数量,请暂时尝试用整数替换,例如:

    //if you're expecting 5 markers
    for(int i=0; i < 5 ; i++){
         animateCarsInMarkers(mark, bearing, latLng, i);
    }
    

    如果所有标记都旋转了,这意味着你的 size 变量只有 1 的值。

    【讨论】:

    • 感谢您的解决方案。这个对我有用。我纠正了我的错误,这只是因为尺寸。但仍然只有一个标记是动画的。您能否建议我任何可能的方法来同时为一组标记设置动画。 @newguy
    • 不要忘记接受或投票以表明它对您有用。现在,对于后续问题,SO thread 可能会有所帮助。包含视频演示和代码。
    • @Manikandan.S 你有解决方案吗?我也在做同样的事情。我无法理解如何做到这一点?你能帮帮我吗?
    猜你喜欢
    • 2015-05-12
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多