我正在用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);
}
答案 0 :(得分:0)
DisplayMetrics dm = new DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(DM); int width = dm.widthPixels; int height = dm.heightPixels; int dens = dm.densityDpi; double wi =(double)width /(double)dens; 双喜=(双)高/(双)窝; double x = Math.pow(wi,2); double y = Math.pow(hi,2); double screenInches = Math.sqrt(x + y);
解释here
尝试这个..让我知道它是否工作......也会告诉你一些其他的选择......