2

我有麻烦。我需要添加并显示几个图像(之前将计算数量)以延迟布局,但它们出现在同一时刻。

这是我的代码:

处理程序:

h = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.d(LOG_TAG, "handle message!");
            int delay = msg.arg2;
            try {
                Thread.currentThread().sleep(delay);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Position position = (Position)msg.obj;
            int img = msg.arg1;
            int topMargin = getMarginInPixels(position.getY());
            int leftMargin = getMarginInPixels(position.getX());
            ImageView iv = new ImageView(MainActivity.this);
            iv.setImageResource(img);
            RelativeLayout.LayoutParams lp = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.topMargin = topMargin;
            lp.leftMargin = leftMargin;
            iv.setLayoutParams(lp);
            rl.addView(iv); 
        }
    };

在循环中:

Message m = h.obtainMessage(0, img, delay, newPosition);
            h.sendMessage(m);

谢谢你的帮助!

4

1 回答 1

0

处理程序与 UI 一起工作,尝试停止后台线程,并从中调用处理程序。像

new Thread( new Runnable(){
 for (int i=0;i<10;i++){
   SystemClock.sleep(1000);//
   Message m = h.obtainMessage(0, img,newPosition);
            h.sendMessage(m);
 }
}
).start();
于 2012-09-16T17:25:22.153 回答