我正在尝试在屏幕上移动图像,但有时图像会显示在屏幕的一半或完全关闭。 我的代码如下:
width = image.getWidth();
height = image.getHeight();
main = (RelativeLayout) findViewById(R.id.mainLayout);
screenWidth = main.getWidth();
screenHeight = main.getHeight();
xCalc = screenWidth - width;
yCalc = screenHeight - height;
imageParams = (RelativeLayout.LayoutParams) image.getLayoutParams();
Random rand = new Random();
xValue = (int) (rand.nextFloat() * xCalc);
yValue = (int) (rand.nextFloat() * yCalc);
imageParams.setMargins(xValue, yValue, 0, 0);
image.setLayoutParams(dotParams);