如何在DP Android Studio中获取X和Y轴的X和Y轴

发布于 2025-02-07 03:36:48 字数 427 浏览 1 评论 0原文

我有一个文本视图,需要每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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2025-02-14 03:36:48

尝试邮政方法

textview.post(new runnable{
Setx(generateXandY())
})

Try with post method

textview.post(new runnable{
Setx(generateXandY())
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文