我有麻烦。我需要添加并显示几个图像(之前将计算数量)以延迟布局,但它们出现在同一时刻。
这是我的代码:
处理程序:
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);
谢谢你的帮助!