Android Java处理程序在循环中的每个记录发布延迟

时间:2015-01-17 12:40:09

标签: java javascript android loops handler

我在尝试像Android Java中的setTimeout()这样的动画时遇到了一些问题。我有一个几何列表,我想一次一个地绘制到地图上:

public void getDirection(Event eventModel, final Context context) {
    String eventX = eventModel.getEventX();
    String eventY = eventModel.getEventY();

    //Code to get data geometry data from API and store in pathGeometries       
    final Drawable d = EventDrawableImage.resizeCurrentLocImage("current_loc",
            context);
    for (int iii = 0; iii < pathGeometries.size(); iii++) {
        final int counter = iii ;
            EventDetail.handler.postDelayed(new Runnable()
            {
                private long time = 0;
                public void run()
                {
                    time += 1000;
                    EventDetail.handler.postDelayed(this, 1000);

                    moveNext(pathGeometries.get(counter).getX(),
                            pathGeometries.get(counter).getY(), 0, d);
                }
            }, 1000); 
    }
}

public static void moveNext(double coordx, double coordy, int k, Drawable d){
    //EventMain.mMapView.removeAll();

    // Set center
    Point p = new Point(coordx, coordy);
    EventMain.mMapView.zoomToResolution(p, 1);

    // Add marker
    PictureMarkerSymbol graphicIcon;
    graphicIcon = new PictureMarkerSymbol(d);
    Symbol symbol = graphicIcon;
    HashMap<String, Object> attrMap = new HashMap<String, Object>();
    attrMap.put("currentLoc", "User Current location");

    EventMain.graphicsLayer.addGraphic(new Graphic(p, symbol, attrMap));     
}

我现在遇到的问题是路径几何传递到moveNext()的每个点,它在移动到另一个点之前不会保持20秒。相反,它只是一直循环直到结束,而不是在每个点停止。

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

不确定我是否理解正确的意图,但尝试这样的事情:

long time = 0;
for (int iii = 0; iii < pathGeometries.size(); iii++) {
    final int counter = iii;
    time += 1000;
    EventDetail.handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            moveNext(pathGeometries.get(counter).getX(),
                    pathGeometries.get(counter).getY(), 0, d);
        }
    }, time);
}

答案 1 :(得分:0)

也许循环并使用循环索引来建立延迟时间。

for (int iii = 0; iii < pathGeometries.size(); iii++) {
            EventDetail.handler.postDelayed(new Runnable()
            {
                public void run()
                {
                    moveNext(pathGeometries.get(counter).getX(),
                            pathGeometries.get(counter).getY(), 0, d);
                }
            }, 1000 *(iii+1)); 
    }