【发布时间】:2016-08-20 16:56:29
【问题描述】:
我希望 myImageView 将自己定位在屏幕内随机位置的某个位置,而不会将文本视图推开。我在另一个线程中找到了这段代码,但我无法让它工作。
myImageView = (ImageView) findViewById(R.id.myImageView);
LinearLayout game = (LinearLayout)findViewById(R.id.game);
int width = game.getWidth();
int height = game.getHeight();
random = new Random();
int x = width;
int y = height;
int imageX = random.nextInt(x-50)+50;
int imageY = random.nextInt(y-100)+100;
myImageView.setX(imageX);
myImageView.setY(imageY);
【问题讨论】:
-
Math.random()是你的朋友。 -
这不是问题。问题是我不知道使用什么方法将图像视图放置在某个位置。
-
我刚刚用我在另一个线程中找到的最新代码更新了主帖。根据他们的说法,这段代码应该可以工作,但由于某种原因它对我不起作用。