简体   繁体   English

如何绘制背景图像并使它在所有Android设备上看起来相似?

[英]How to draw background image and make it looks similar on all Android devices?

Looking for advice with the next problem. 寻找下一个问题的建议。 I am developing small game and I have a *.png file for background. 我正在开发小型游戏,但是我有一个* .png文件作为背景。 I need to put it to background of the main game screen. 我需要将其放在游戏主屏幕的背景上。 Moreover, it has to bee 2 times bigger in width and 1.5 times bigger in height as the screen's sizes, because my objects "flight" across these borders. 此外,它的宽度和屏幕高度必须比屏幕尺寸大2倍,高度要高1.5倍,因为我的物体“飞越”了这些边界。 Additionally the screen is moving around this background in a gameplay. 此外,屏幕还在游戏中围绕此背景移动。 But I have stuck a bit on how to do it. 但是我对如何做到这一点有所保留。

I want my background to look similar on all screens with different sizes and densities. 我希望我的背景在所有尺寸和密度不同的屏幕上看起来都相似。 I have tried some solutions but I don't like them, or I have made something wrong: 我尝试了一些解决方案,但是我不喜欢它们,或者我做错了什么:

  1. Make different background images and put them in special folders. 制作不同的背景图像,并将它们放在特殊的文件夹中。 It sounds good, but with the amount of resolutions of Android devices in the market it is not sounds good for me. 听起来不错,但是鉴于市场上有很多Android设备的分辨率,这听起来对我来说并不好。 It will just make the size of *.apk bigger. 它只会使* .apk的大小更大。 And if I will use the methods to set background it will stretch the image - not a good idea I think. 如果我将使用这些方法设置背景,它将拉伸图像-我认为这不是一个好主意。

  2. In the onDraw() method draw the image on canvas. onDraw()方法中,在画布上绘制图像。 I have to put it's top-left corner to the most top-left corner of the possible game area and draw it. 我必须将其左上角放置到可能的游戏区域的最左上角并进行绘制。 But, here is some options: 但是,这里有一些选择:

    • cut from the main image the image i want and draw only this piece (what I use now) 从主图像上切下我想要的图像,然后仅绘制此部分(我现在使用的是什么)
    • resize the image I have and draw with this changes 调整我的图像大小并进行此更改
    • something else... 其他的东西

So, the question is: what is the best option for drawing background for the game screen when you need it to be almost twice bigger than the screen's size, make it looks same on all devices and move the screen around when the game is played? 因此,问题是:当您需要为游戏屏幕绘制背景尺寸几乎是屏幕尺寸的两倍时,使它在所有设备上看起来都一样并且在玩游戏时将屏幕四处移动时,绘制背景的最佳选择是什么?

you should use 9 patch image . 您应该使用9个补丁图像。

A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. NinePatchDrawable图形是可拉伸的位图图像,Android会自动调整其大小以适应将其放置为背景的View的内容。

see this link. 看到这个链接。 http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何制作类似于Preferences的ListView? - Android: how to make a ListView that looks similar to Preferences? 如何在Android中制作类似于滚动壁纸的背景图片? - How to make a background image similar to scrolling wallpaper in Android? 如何使图像看起来像原样? - How to make an image looks as it is? Android:支持所有设备的背景图像大小(以像素为单位) - Android: Background Image Size (in Pixel) which Support All Devices 如何将图像设置为所有Android设备的背景,以使拉伸不会使图像变形? - How to set image as background for all Android devices so that stretching does not distort it? 如何使应用程序响应所有Android设备? - How to make application responsive for all android devices? 我怎么能编写我的布局代码,以便我的布局看起来对所有Android设备统一? - How could I write my code for layout such that my layout looks uniform for all android devices? Android 添加一个尺寸在所有设备上看起来都相同的内边距 - Android add a padding with size that looks equal on all devices 如何在Android上绘制带有“结尾”的平铺背景图像? - How can I draw a tiled background image with “ends” on Android? 如何使背景图像在 android 中填充全屏? - How to make background Image to fill fullscreen in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM