如何在DP Android Studio中获取X和Y轴的X和Y轴
我有一个文本视图,需要每1秒更改其在屏幕上的位置。我有一种方法可以使我在屏幕的最大高度/宽度之间给我一个随机值。问题是我要硬编码值,这在具有较小或更大屏幕的其他设备上无法使用。关于如何做到这一点的任何建议,无论屏幕的大小如何,我的观点仍然会在不屏幕外的情况下改变其位置?我也尝试了其他方法/功能,但它们不起作用或不弃用。这是函数/方法的代码:
public float generateXAndY(int x, int y) {
Random rnd = new Random();
return rnd.nextInt((y - x)) + x;
}
textViewEgnDisplay.setX(generateXAndY(0,1570));
textViewEgnDisplay.setY(generateXAndY(0,782));
I have a text view onTick that needs to change its position on the screen every 1 sec. I have a method to give me a random value between 0 and the max height/width of the screen. The issue is I'm hardcoding the values and this won't work on other devices with a smaller or bigger screen. Any advice on how to make that no matter the size of the screen my view will still be changing its position without going off-screen? I have tried other methods/functions as well but they don't work or/are deprecated. Here is the code of the function/method:
public float generateXAndY(int x, int y) {
Random rnd = new Random();
return rnd.nextInt((y - x)) + x;
}
textViewEgnDisplay.setX(generateXAndY(0,1570));
textViewEgnDisplay.setY(generateXAndY(0,782));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试邮政方法
Try with post method