简体   繁体   中英

Wait until method finish inside for loop android

I do have this following for loop

The array sections has a list of locations "LatLng" for each section inside the loop i'm calling animateMarker which animate the marker in my map like 2 sec

My problem is that this loop goes so fast that I can't even see the animate for each section . it just show the last section from the array and also sometimes it freez the app

The animatemarker has a hundler inside it with post delay

I want to see each animateMarker for each section in my loop I'm nooby with threads so I really do need help

Problem here

        ArrayList<LatLng> sections = gd.getSection(mDoc);
            for (int j = 0; j < sections.size(); j++) {
                animateMarker(busMarker, new LatLng(sections.get(j).latitude, sections.get(j).longitude), false);
            }

my animateMarker ( has no problem works fine ! )

 public void animateMarker(final Marker marker, final LatLng toPosition,
                              final boolean hideMarker) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        Projection proj = mMap.getProjection();
        Point startPoint = proj.toScreenLocation(marker.getPosition());
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
        final long duration = 2000;

        final Interpolator interpolator = new LinearInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);
                double lng = t * toPosition.longitude + (1 - t)
                        * startLatLng.longitude;
                double lat = t * toPosition.latitude + (1 - t)
                        * startLatLng.latitude;
                marker.setPosition(new LatLng(lat, lng));

                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                } else {
                    if (hideMarker) {
                        marker.setVisible(false);
                    } else {
                        marker.setVisible(true);
                    }
                }
            }
        });
    }

I dont know if there is proper ways, but this is the way i should do,

You can open a new thread and call your forloop in that thread. You also need to play with maps api to make animation 2 seconds and let the runnable object be single instance . So it will be more memory friendly.

for (int j = 0; j < sections.size(); j++) {
    synchronized (this) {
      runOnUiThread(new Runnable() 
      {
         public void run() 
         {
           animateMarker(busMarker, new LatLng(sections.get(j).latitude, sections.get(j).longitude), false);
         }
       });              
    }
    sleep(2000);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM