Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

java - Stop / Pause an Iterator

I am trying to implement an animation of Markers on a Google Map. The issue I am running into is an Animation being started while another Animation is still happening for a specific Marker.

Is there a way to stop the iteration of a loop, and continue only when a certain criteria is met?

Get LatLng - Start Animation - Callback Fires (Animation Finished) - Get LatLng.. Etc.

public void animateMarker(final String key, final List<LatLng> latlngList) {
    Log.e(TAG, "------------- MARKER " + key + "-------------");

    Handler mHandler = new Handler();
    mHandler.post(new Runnable() {

        final AnimateCarObj animateCarObj = animateCarObjMap.get(key);
        final Marker marker = markersHashMap.get(key);
        Boolean isAnimationRunning = false;

        @Override
        public void run() {
            final Iterator<LatLng> iterator = latlngList.iterator();
            while (iterator.hasNext()) {
                if (!(isAnimationRunning)) {
                    Log.e(TAG, "START -- " + key + ": " + iterator.next().toString());
                    try {
                        isAnimationRunning = true;
                        LatLngInterpolator latlonInter = new LinearFixed();
                        latlonInter.interpolate(1, marker.getPosition(), iterator.next());
                        MarkerAnimation.animateMarker(new RunningCallback() {
                            @Override
                            public void onFinish() {
                                Log.e(TAG, "FINISH -- " + key + ": " + iterator.next().toString());
                                isAnimationRunning = false;
                            }
                        }, latlngList.size(), marker, iterator.next(), latlonInter);

                    } catch (Exception e) {
                        Log.e(TAG, "EXCEPTION: " + e.getMessage());
                        e.printStackTrace();
                    }

                }
            }

        }
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use a while loop to stop your iterator logic.

while (iterator.hasNext()) {
    // wait
    while (!someBooleanConditionToWaitFor) { }

    // do your other stuff
    // when you're done your other stuff, set it back to false.
    someBooleanConditionToWaitFor = false;
}

When your animation has finished, set the condition to true so that it continues. When you're at the end of that iteration, set it back to false so it waits again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...