我希望你能帮助我。我需要两个循环。它们必须同时运行,一个循环需要延迟。第一个循环必须在Array中创建ImageViews,第二个循环必须获取视图上的位置。我知道它必须用Threads创建但是如何?或者换一种方式呢?
public ImageView[] bubbles = new ImageView[];
public void create_bubbles() throws InterruptedException {
RelativeLayout game_layout = (RelativeLayout) findViewById(R.id.game_layout){
ThreadForLoopA threadA = new ThreadForLoopA();
ThreadForLoopB threadB = new ThreadForLoopB();
threadA.start();
threadB.start();
}
}
public class ThreadForLoopA extends Thread{
float posX = 10;
float posY = 10;
@Override
public void run(){
for(int i=0;;i++){
bubbles[i] = new ImageView(null);
bubbles[i].setImageResource(R.drawable.unbenannt);
game_layout.addView(bubbles[i], 100, 100);
setPos(posX, posY, bubbles[i]);
posX = posX + 100;
if(i == 5){
break;
}
}
}
}
public class ThreadForLoopB extends Thread{
@Override
public void run(){
for(int i = 0 ; ; i++){
float location;
location = bubbles[i].getY();
if(i == 5){
break;
}
}
}
}
}
答案 0 :(得分:0)
您可以从Handler发布延迟,但是对于您的情况,您必须在ThreadA完成后启动ThreadB,否则您将无法获得值。
延迟执行:
RelativeLayout game_layout = (RelativeLayout) findViewById(R.id.game_layout){
ThreadForLoopA threadA = new ThreadForLoopA();
final ThreadForLoopB threadB = new ThreadForLoopB();
threadA.start();
new Handler().postDelayed(new Runnable(){
public void run(){
threadB.start();
}}, 5000);//5 seconds
}