如何在 Android 中缩放 View.Canvas 上的位图?
我有一个 View 派生类,可以在 onDraw
中直接在画布上绘制 790x500(是的,这很奇怪,但必须保持这种方式)位图。这在我的 Nexus S 上看起来没问题,但是当我在屏幕较小的设备(例如 Wildfire)上运行我的应用程序时,我只能看到位图的左上部分。我在这里缺少什么?
这是一个类似游戏的应用程序,我在某些坐标上绘制许多位图。我真的不想对 LDPI、MDPI 和 HDPI 有不同的像素设置(我有充分的理由这样做)。
问:如何针对任何屏幕分辨率正确缩放位图? (对于大屏幕,将游戏屏幕居中可能是一种选择,但不是必须的。)例如,当我在 320x240 屏幕上绘制 800x600 图像时,图像会自动拉伸,并且当我输出像素时100x100 时,它变为 40x40。
I have a View-derived class where I draw a 790x500 (yes, this is odd but it has to stay this way) bitmap directly on the canvas in onDraw
. This looks ok on my Nexus S, but when I run my app on a device with a smaller screen (e.g. Wildfire) I only see the upper left part of the bitmap. What am I missing here?
This is a game-like app where I draw a number of bitmaps on certain coordinates. I really don't want to have different pixel setups for LDPI, MDPI and HDPI (I have a good reason for this).
Q: How can I properly scale my bitmaps for any screen resolution? (Centering the game screen for large screens may be an option, but is not a must.) E.g. when I draw a 800x600 image on a 320x240 screen, the image is automatically stretched and when I output a pixel at 100x100, it becomes 40x40.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
drawbitmap() 方法有多种形式。选择允许您指定源矩形和目标矩形的选项。源矩形将是位图的大小,目标矩形将缩放以适合设备显示屏。 Android 在绘制位图时会对其进行缩放。
您还可以缩放和平移(移动)整个画布
There are many forms of the drawbitmap() method. Choose the one that allows you to specify the source and destination rectangle. The source rectangle will be the size of your bitmap and the destination rectangle will be scaled to fit on the device display. Android will scale your bitmap when it draws it.
You can also scale and translate (shift) the entire canvas
Canvas.scale()
就可以了。Canvas.scale()
does the trick.