画布位图绘制和屏幕密度

发布于 2024-12-13 05:43:18 字数 864 浏览 4 评论 0原文

我正在开发一个基于 SurfaceView 类的 android 游戏。目前我一直在尝试优化不同屏幕尺寸的视图。

问题 1:我的 res 文件夹中有 PNG 文件,我使用 Canvas.drawBitmap 绘制它们。阅读本文后(http://developer.android.com/guide/practices/screens_support. html )我认为如果屏幕更大的话 android 会调整位图的大小。不幸的是,与在手机上启动我的应用程序相比,在平板电脑上启动我的应用程序时,我发现没有缩放(或任何更改)。那么它是如何做到的呢?我是否应该想出一些奇特的策略来计算可用空间并使用源矩形和目标矩形绘制位图?如果有人有最佳实践或一些提示如何在画布上绘制位图,那就太好了。我已经阅读了 Pixel<->dip 转换主题,但我不得不说我不知道​​如何在我的应用程序中使用该“解决方案”。所以感谢任何帮助...

问题 2:我的 res 文件夹中有 3 个 PNG。 1 个 ldpi、1 个 mdpi 和 1 个 hdpi。 Ldpi 图像为 50px*50px,mdpi 为 75px*75px,hdpi 为 100px*100px。当我在模拟器(800*480)中加载应用程序时,它使用 100*100 png。当我在 Acer Iconia 选项卡上加载应用程序时,它使用 75*75 图像。我认为 hdpi 适用于大屏幕(= tab),而 mdpi 或 ldpi 将用于模拟器屏幕。至少我认为android也会在选项卡上使用hdpi图像,但我很惊讶...对此的任何解释也非常感谢

抱歉4长文本,只是想弄清楚...

cya 埃夫迪

i'm developing a game for android based on the SurfaceView class. Currently i'm stuck trying to optimize the view for different screen-sizes.

Problem 1: i have PNG-Files in my res folder and i draw them using Canvas.drawBitmap. After reading this article ( http://developer.android.com/guide/practices/screens_support.html ) i thought android resizes the bitmaps if the screen is bigger. Unfortunately i recognized no scaling (or any changes) when launching my app on a tablet vs. launching it on a phone. So how is it done? Should i come up with some fancy strategy to calculate the available space and draw my bitmaps using the source- and destination-Rectangle? Would be nice if someone has a best practice or some hints how to draw bitmaps on a canvas. I already read the pixel<->dip conversion topics but i have to say i don't know how to use that "solution" in my app. So any help appreciated...

Problem 2: I have 3 PNGs in my res folder. One ldpi, one mdpi and one hdpi. Ldpi image has 50px*50px, mdpi has 75px*75px and hdpi has 100px*100px. When i load the app in the emulator (800*480) it uses the 100*100 png. When i load the app on the Acer Iconia Tab it uses the 75*75 image. I thought hdpi is for big screens (=tab) and mdpi or ldpi would be used for the emulator screen. At least i thought android would use the hdpi-image also on the tab but i got surprised... Any explantation for this as well much appreciated

Sorry 4 long text, just trying to be clear...

cya
EffDee

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

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

发布评论

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

评论(2

_失温 2024-12-20 05:43:18

要计算您的绘图应该有多大,您可以使用 Canvas.getDensity() 。

屏幕的 DPI 值取决于像素数量和屏幕尺寸。平板电脑确实具有高分辨率,但大多数也很大。这会导致 DPI 值降低。有关更多信息,请参阅 http://developer.android.com/guide/practices/ screen_support.html

For calculating how big your drawing should be, you can use Canvas.getDensity().

The DPI value of a screen depends on the amount of pixels and on the size of the screen. Tablets do have a high resolution, but most are quite large aswell. This leads to a lower DPI value. For more information have a look at http://developer.android.com/guide/practices/screens_support.html

飞烟轻若梦 2024-12-20 05:43:18

最佳实践是将图像放入 mdpi 文件夹(像素宽高比 1),然后使用所有水平尺寸的密度。
例如:

src.left = offsetX * density; 
src.top = 0; 
src.right = (offsetX + width - 1) * density; 
src.bottom = height - 1;
dst.left = x * density; 
dst.top = y; 
dst.right = (x + width - 1) * density; 
dst.bottom = height - 1;
canvas.drawBitmap(myImageStrip, src, dst, null);

对于 mdpi 密度为 1.0,ldpi 密度=0.75,对于 hdpi 密度=1.5
您可以使用以下方法获取这些值和其他值

density = getResources().getDisplayMetrics().density;

还有另一种解决方案:您可以将相同的图像放在所有文件夹中。系统将采用正确的值,因此您无需将值与密度相乘。但在这种情况下,您将需要同一图像的 3 个或更多副本。

The best practice is to put images into mdpi folder (pixel aspect 1) and then use density with all horizontal dimensions.
For example:

src.left = offsetX * density; 
src.top = 0; 
src.right = (offsetX + width - 1) * density; 
src.bottom = height - 1;
dst.left = x * density; 
dst.top = y; 
dst.right = (x + width - 1) * density; 
dst.bottom = height - 1;
canvas.drawBitmap(myImageStrip, src, dst, null);

For mdpi density is 1.0, ldpi density=0.75 and for hdpi density=1.5
You can obtain these and other values using

density = getResources().getDisplayMetrics().density;

There is another solution: you can put same image in all folders. The system will take proper one, so you dont need to multiply values with density. But in that case, you'll need 3 or more copies of same image.

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