[英]Wait until method finish inside for loop android
I do have this following for loop 我确实有以下for循环
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 数组部分为循环内的每个部分提供了一个位置“ LatLng”的列表,我正在调用animateMarker,该动画使我的地图中的标记动起来,例如2秒
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 animatemarker的内部带有杂音器,且延迟发布
I want to see each animateMarker for each section in my loop I'm nooby with threads so I really do need help 我想查看循环中每个部分的每个animateMarker,因为我对线程不满意,所以我确实需要帮助
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 ! ) 我的animateMarker(没问题的很好!)
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. 您可以打开一个新线程,然后在该线程中调用forloop。 You also need to play with maps api to make animation 2 seconds and let the runnable object be single instance .
您还需要使用maps api进行动画制作2秒,并使可运行对象成为单个实例 。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.