[英]Google Maps v2 addPolygon() plugs up UI thread so my progress dialog doesn't animate
I am working on an application in which I would like to be able to load up to 10,000 polygons (40,000 lat/lng pairs) onto Google Maps v2 and the performance is mostly acceptable. 我正在开发一个应用程序,在该应用程序中,我希望能够将多达10,000个多边形(40,000个纬度/经度对)加载到Google Maps v2上,并且性能几乎可以接受。 But since it takes about 20 seconds to load, I need to have a progress indicator so it doesn't look like the app has locked up.
但是由于加载大约需要20秒,因此我需要有一个进度指示器,以使它看起来好像应用程序没有锁定。
I believe the core issue is that the map.addPolygon(polygonOptions) call which I must repeat 10,000 times and must run on the UI thread plugs up the UI thread such that the progress dialog doesn't get any time to update it's UI. 我相信核心问题是我必须重复10,000次并且必须在UI线程上运行的map.addPolygon(polygonOptions)调用会堵塞UI线程,这样进度对话框就没有时间更新其UI。 Here is an approximation of my code:
这是我的代码的近似值:
@Override
protected void onCreate(.....) {
progressDialog = new ProgressDialog(AllLayersActivity.this);
progressDialog.setMessage("Processing files:");
progressDialog.setTitle("Loading data");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.setMax(1);
progressDialog.show();
progressDialog.incrementProgressBy(1);
for(String filename : intent.getStringArrayListExtra(DbxService.PARAM_FILES)) {
if(filename.toLowerCase(Locale.getDefault()).endsWith(".shp")) {
dbxService.getFileContents(new DbxPath(pathToField + filename));
}
}
}
BroadcastReceiver for dbxService.getFileContents() {
new Thread(new Runnable() {
@Override
public void run() {
byte[] fileContents = loadFromBroadcastIntent(.....);
progressDialog.setMax(progressDialog.getMax() + getNumberOfParts(fileContents));
// bunch of other processing
for(Part part : getParts(fileContents) {
final PolygonOptions polygonOptions = new PolygonOptions();
// processing stuff
polygonOptions.addAll(latLngList);
runOnUiThread(new Runnable() {
public void run() {
map.addPolygon(polygonOptions);
progressDialog.incrementProgressBy(1);
if(progressDialog.getProgress() == progressDialog.getMax()) {
progressDialog.dismiss();
}
}
});
}
}
}).start();
}
I know that increasing the max of ProgressDialog on the fly probably looks kind of weird but I think it should work well in practice as the progressDialog.setMax(progressDialog.getMax() + parts);
我知道即时增加ProgressDialog的最大值可能看起来有点怪异,但我认为它在实践中应该可以很好地工作,因为
progressDialog.setMax(progressDialog.getMax() + parts);
line runs many times less than the progressDialog.incrementProgressBy(1);
该行的运行次数比
progressDialog.incrementProgressBy(1);
少很多倍progressDialog.incrementProgressBy(1);
lines, always adds more before the dialog could possibly falsely get to 100%, and should be increased to its final value quite quickly because the map.addPolygon(polygonOptions);
行,总是在对话框可能错误地达到100%之前添加更多,并且应该很快增加到其最终值,因为
map.addPolygon(polygonOptions);
line is by far the slowest thing I'm doing. 到目前为止,这是我做得最慢的事情。
So, what can I do to allow the progress bar to animate nicely? 那么,我该怎么做才能让进度条很好地进行动画处理?
I would have just added this as a comment but I have under 50 rep so I can't yet. 我将添加此内容作为评论,但我的代表不到50个,所以还不能。 With that said I have an app that can add up to 10,000 Markers on the map and I run into the same issue.
话虽如此,我有一个应用程序可以在地图上添加多达10,000个标记,而我遇到了同样的问题。 I tried adding the Markers in another thread but adding items to the map must be done in the UI thread which is the same thread as the progressDialog.
我尝试在另一个线程中添加标记,但是必须在UI线程中将项目添加到地图中,该UI线程与progressDialog相同。 So the short answer is No. If someone out there smarter than me knows of away to do this in an AsyncTask or Runnable then please LET ME KNOW TOO!
因此,简短的答案是“否”。如果在那里有人比我更聪明,那么可以在AsyncTask或Runnable中做到这一点,那么请让我知道!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.