【发布时间】:2014-05-04 18:19:30
【问题描述】:
我正在用 Java 编写一个应用程序,我想在屏幕上随机放置一个按钮。 我使用了 Display.getSize(),但它为我提供了屏幕的整个大小,包括返回、主页和最近活动按钮的底部栏。所以我随机位于屏幕中的按钮通常隐藏在这个栏下。在我的计算中如何考虑这个栏?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hiding_buttons);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
SCREEN_WIDTH = size.x;
SCREEN_HEIGHT = size.y;
}
public void button_hideButtonClick(View view) {
// init button location to beginning position
view.offsetLeftAndRight(-horizonOffset);
view.offsetTopAndBottom(-verticalOffset);
Random r = new Random();
// generate random pos relative to the screen
horizonOffset = r.nextInt(SCREEN_WIDTH-view.getWidth());
verticalOffset = r.nextInt(SCREEN_HEIGHT-view.getHeight());
// relocate the button randomly
view.offsetLeftAndRight(horizonOffset);
view.offsetTopAndBottom(verticalOffset);
}
【问题讨论】:
标签: java android random screen