如何在 Android 上绘制完整的 800 x 480 图像
我有一个 800 x 300 的图像,我知道这是我的测试平台分辨率的宽度(HTC Desire 为 800x480)。当我尝试将此图像绘制到屏幕上时,它会奇怪地缩放。它溢出了屏幕的左侧并几乎填满了整个垂直方向。
我正在使用这样的代码:
canvas.drawBitmap( screen[1], new Rect(0,0,800,300), new Rect(0,0,800,300), null);
出于某种原因,
width_x = canvas.getWidth();
width_y = canvas.getHeight();
我的分辨率报告为 533 x 320。现在我假设这是 Android 使用的令人着迷的缩放系统,因此应用程序在所有手机上显示相同的大小但我想忽略这一点。我正在编写一个游戏,因此想要自己处理缩放和定位 - 例如,如果可用的话,使用更多的屏幕空间。最佳 Android 实践可能适合基于图标的应用程序,但我想绘制到绝对像素位置,并获取屏幕的绝对分辨率信息。
因此我的问题是——这绝对不可能吗?如果它是完全禁忌的,因为 Android 有一个简单而有效的系统来执行此操作,那么我有兴趣知道它是什么。 Dpi 与我的游戏设计无关(就像这是一款电脑游戏,那就无关紧要)
也许我的屏幕实际上是 533 x 320,除非我以某种方式指定分辨率?我尝试使用宽度和高度的缩放值,并且图像在屏幕上的大小大致正确,但由于发生了某种缩放而具有锯齿状边缘。因此,我无法访问屏幕能够显示的所有像素。
为了缩放它,我使用了
canvas.drawBitmap( screen[1], new Rect(0,0,800,300), new Rect((int)(0.0f),(int)(0.0f),(int)(533.0f),(int)(187.5f)), null); // numbers
“Just whacked in for test”之类的东西 - 比率相当于报告的屏幕分辨率。看起来很可怕。
I've got an image that is 800 by 300, which I know is the width of my test platform's resolution (HTC Desire at 800x480). When I try to draw this image to the screen it scales oddly. It spills over the left hand side of the screen and fills almost all the vertical.
I'm using code like this:
canvas.drawBitmap( screen[1], new Rect(0,0,800,300), new Rect(0,0,800,300), null);
For some reason
width_x = canvas.getWidth();
width_y = canvas.getHeight();
reports my resolution as 533 by 320. Now I assume this is for the expletive-deleted fascinating scaling system Android uses so apps appear the same size on all phones but I want to ignore this. I'm writing a game, so want to handle scaling and positioning myself - for instance using more screen estate if it becomes available. Best Android practice may be suitable for an icon based application, but I would like to draw to absolute pixel positions, and get absolute resolution information for the screen.
Therefore my question is this - is this absolutely impossible? If it is completely contraindicated because Android has a simple and effective system in place to do this then I would be interested to know what it is. Dpi is not relevant to my game design (just like if this was a pc game, it would be irrelevant)
Perhaps my screen actually 533 by 320 unless I specify a resolution somehow? I tried using the scaling values from width and height and the image was the correct size on screen, roughly, but had jagged edges because some sort of scaling had occurred. I therefore did not have access to all the pixels my screen is capable of displaying.
To scale it I used something like
canvas.drawBitmap( screen[1], new Rect(0,0,800,300), new Rect((int)(0.0f),(int)(0.0f),(int)(533.0f),(int)(187.5f)), null); // numbers
Just whacked in for testing - ratio equivalent to reported screen resolution. Looks horrid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Android 不会做任何事情来扰乱你感知的分辨率——你在 Desire 上使用的是 800x480 像素。
您是否在全屏、自定义视图中工作并覆盖 onDraw?您正在使用 SurfaceView 吗?我们需要先了解这些信息,然后才能帮助您解决问题。
假设您正在执行上述操作,您应该能够使用 Canvas.drawBitmap(位图位图,左浮动,上浮动,Paint 绘制)。在您的情况下,它看起来像
canvas.drawBitmap( screen[1], 0.0f, 0.0f, null);
将其放在画布的左上角。在性能敏感的应用程序(如游戏)中,您不希望在绘制循环期间使用源/目标 Rect 版本的drawBitmap(),因为它将在循环的每次迭代期间进行缩放。相反,将原始位图的缩放/裁剪版本绘制到另一个成员位图,并使用上面链接的 drawBitmap 的 x/y 偏移版本在循环中绘制该位图。
如果您想知道正在使用的屏幕空间量(如果您正在进行任何自定义绘图,那么您绝对应该这样做),您将需要覆盖 onSizeChanged() 或 surfaceChanged(),取决于实现。
您确实应该查看 API 演示,其中有一些很好的示例说明如何准确地做你想做的事。
祝你好运!
Android is not doing anything to mess with your perceived resolution - you are working with 800x480 pixels on that Desire.
Are you working in a fullscreen, custom View and overriding onDraw? Are you working with a SurfaceView? We need to know these things before we can help you with your problem.
Assuming you are doing the above, you should be able to draw your bitmap to the screen without any scaling using Canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint). In your case, that would look something like
canvas.drawBitmap( screen[1], 0.0f, 0.0f, null);
to put it in the upper left corner of your canvas.In performance-sensitive apps (like games), you don't want to use the source/destination Rect version of drawBitmap() during your draw loop, since it will do the scaling during every iteration of the loop. Instead, draw a scaled/cropped version of your original bitmap to another member bitmap, and draw that one in the loop, using the x/y offset version of drawBitmap linked above.
If you want to know the amount of screen real estate you're working with (and you definitely should, if you're doing any custom drawing), you'll want to override either onSizeChanged() or surfaceChanged(), depending on implementation.
You should really check out the API demos, there are some great examples of how to do exactly what you're trying to do in there.
Good luck!
我解决了我的问题之一 - 在清单文件中我所针对的操作系统设置不正确 - 将其切换到 4(即 1.6)似乎可以修复我获得的高度和宽度的值,至少对于 HTC 来说是这样。模拟器的问题比较多,但至少它是朝着正确方向迈出的重要一步。供您参考,我正在全屏、横向模式(固定)下工作,几乎所有内容都覆盖了功能。 (包括 onDraw、surfaceChanged 等)
如果我可以获得绝对宽度和高度,我可以编写自己的代码来加载正确的资源并使用正确的缩放来进行屏幕定位 - DPI 不是问题,因此希望不会出现问题不要偏离建议的指导方针太远。
I fixed one of my problems - in the manifest file the OS I was targeting was set up incorrectly - switching it to 4 (i.e. 1.6) seemed to fix the values I was getting for height and width, at least for the HTC. Emulator is more problematic, but at least its a major step in the right direction. For your info, I'm working in full screen, landscape mode (fixed), with overridden functions for pretty much everything. (Including onDraw, surfaceChanged, and so forth)
If I can get the absolute width and height I can write my own code for loading the correct assets and using the correct scaling for screen positioning - DPI isn't an issue so hopefully that won't stray too far from suggested guidelines.