我想讓ImageView導彈1在屏幕右邊緣的隨機y座標處產生,然後將它左移到屏幕上直到它離開屏幕,最後將其移回屏幕右邊緣的隨機y座標,以無限循環重複該過程。代碼我生成的圖像在一個完全隨機的x和y座標位置,每當活動重新啓動,但它不會移動。任何人都可以明白爲什麼它不動併爲我的問題提供解決方案?一些示例代碼將不勝感激。Android:移動ImageView以更新線程中的值
產生的X線程和y座標值:
public class Missiles extends Thread {
private int height, width, currentscore;
private ImageView missile1, missile2, missile3, missile4, missile5;
Handler updatemissile = new Handler();
int min = 100;
RelativeLayout layout;
int setx1, sety1, setx2, sety2, setx3, sety3, setx4, sety4, setx5, sety5;
private Random rand = new Random();
TextView x, y;
Missiles(int screenheight, int screenwidth, ImageView missile01, ImageView missile02, ImageView missile03,
ImageView missile04, ImageView missile05, RelativeLayout rl, TextView xtext, TextView ytext) {
missile1 = missile01;
missile2 = missile02;
missile3 = missile03;
missile4 = missile04;
missile5 = missile05;
height = screenheight;
width = screenwidth;
layout = rl;
x = xtext;
y = ytext;
}
public void updatevalue(int value) {
currentscore = value;
}
public void run() {
SetMissilePosition position = new SetMissilePosition(missile1, layout, x, y);
updatemissile.post(position);
//width is the width of the screen as height is the height of the screen
while (true) {
setx1 = width;
position.updateX(setx1);
int randomNum = rand.nextInt((height - min) + 1) + min;
sety1 = randomNum;
position.updateY(sety1);
while (setx1 > -50) {
setx1 = setx1 - 1;
position.updateX(setx1);
}
setx1 = width;
position.updateX(setx1);
}
}
}
了Runnable類:
public class SetMissilePosition implements Runnable{
int x1,y1, updateindicator;
ImageView missile1;
RelativeLayout layout;
TextView x,y;
SetMissilePosition(ImageView missile01, RelativeLayout relativeLayout, TextView xtext, TextView ytext) {
missile1 = missile01;
layout = relativeLayout;
x = xtext;
y = ytext;
}
public void updateX(int setx1) {
x1 = setx1;
updateindicator = 1;
}
public void updateY(int sety1) {
y1 = sety1;
updateindicator = 1;
}
public void run() {
missile1.setImageResource(R.drawable.missileanim);
layout.addView(missile1);
if(updateindicator == 1) {
missile1.setX(x1);
missile1.setY(y1);
x.setText(Integer.toString(x1));
y.setText(Integer.toString(y1));
updateindicator = 0;
}
}
}
如果我以任何方式不清晰,或者您需要更多的代碼,比如我的MainActivity,請問。提前致謝!
關於翻譯動畫,是否有可能在轉換過程中檢測Imageview碰撞?我相信這是不可能的,所以我不能使用它。我會研究runOnUiThread。謝謝。 – DavidPrabhu
我不認爲在轉換過程中嚴格檢測imageview碰撞是可能的,但這不應該是個問題。當你初始化變形動畫時,你知道起點和終點(以及移動物體的速度)是什麼。那時,路徑完全是確定性的 - 所以你可以確定它是否會提前發生。 – Eric
我的應用程序需要實時玩家輸入,無法估計。 – DavidPrabhu