以多种屏幕分辨率、密度填充整个屏幕

发布于 2024-10-13 00:44:18 字数 811 浏览 9 评论 0原文

我想在所有 Android 手机上绘制一个填充整个屏幕的矩形,所有分辨率,所有屏幕密度。一切......:) 首先,根据我的理解,Canvas.drawRect 用作​​像素尺寸值。希望这是正确的。 作为 Android 新手,我认为如果我简单地这样写就足够了,因为目前没有分辨率高于 1024x1024 像素的 Android 设备:

ScreenWidth = 1024;
ScreenHeight = 1024;
canvas.drawRect(0,0, ScreenWidth, ScreenHeight, mLoadPaint);

但似乎在某些设备上这个 Rectangle 并没有填充整个屏幕。奇怪的是,我无法解释自己的是,这种情况并不总是发生,而是在某个时间点发生...... 无论如何,代码似乎有问题,所以我想将其更改为如下所示,但现在可以肯定,metrics.widthPixels和metrics.heightPixels将解决我的问题:

wm = (WindowManager) getSystemService(WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
canvas.drawRect(0,0, metrics.widthPixels, metrics.heightPixels, mLoadPaint);

我想知道这是否正确,并且如果这段代码将覆盖所有屏幕,则矩形将填满整个屏幕。 当然,使用这种方法时,我还必须注意屏幕方向,因为当屏幕处于横向时,它们之间的值似乎会交换。

I want to draw a rectangle that fills the entire screen on all Android phones, all resolution, all screen densities.Everything... :)
First from what I understand Canvas.drawRect use as dimensions values in pixel. Hope this is correct.
Being new to Android I thought that if I simply write like this it will be sufficient, since at this moment there is no Android device with a resolution higher than 1024x1024 pixels:

ScreenWidth = 1024;
ScreenHeight = 1024;
canvas.drawRect(0,0, ScreenWidth, ScreenHeight, mLoadPaint);

But it seems that on some devices this Rectangle doesn't fill the entire screen. What is strange and I can't explain myself is that this doesn't happens always but at a point in time...
Anyway it seems that there is a problem with the code so I would like to change it to look something like this, but now sure it metrics.widthPixels and metrics.heightPixels will solve my problem:

wm = (WindowManager) getSystemService(WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
canvas.drawRect(0,0, metrics.widthPixels, metrics.heightPixels, mLoadPaint);

I want to know if this is correct, and if this piece of code will cover all screens, and the rectangle will fill the entire screen.
Of course with this approach I will have to take care also of the screen orientation, as it seems that the values are swapped between them when the screen is in Landscape.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

七颜 2024-10-20 00:44:18

你可以尝试canvas.drawColor(int color)

You can try canvas.drawColor(int color)

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