I am using pygames for android to write a small game.
Therefore I request a screen of the size 960x540:
screen = pygame.display.set_mode((WIDTH, HEIGHT))
background = pygame.image.load("floor.png").convert()
backgroundrect = background.get_rect()
and in the main loop I draw a background image of the same size:
screen.blit(background, backgroundrect)
On my development machine everything is correct since the created window matches the requested size.
What I get when I run on android is my background image centered in a window with different aspect ratio. This would not be so bad but in the space where no image should be drawn I see random garbage drawn. I can even fill the background with black beforehand and still see garbage.
Somehow the draw of the background image also draws in the space outside of the aspect ratio?
The bug is also described here: http://pygame.renpy.org/forum/viewtopic.php?f=4&t=624&p=4915#p4915 which contains a video showing the effect.
I assume this is caused by a wrong window size.
You should look into pygame.display.Info (http://www.pygame.org/docs/ref/display.html#pygame.display.Info) to get the screen resolution.
See this for more details: http://pygame.renpy.org/forum/viewtopic.php?f=4&t=92#p265
You obviously have to consider this when blitting, as rather than the PyGame window being stretched to fit a larger screen, the PyGame window itself is being made larger.
Related
I am working on a widget, where I need to have a chart displayed in addition to some text fields. I have figured out how to generate a visualization of the graph and then convert it to a bitmap, which gets placed in the ImageView. The problem is the following:
In a normal Android app, I would get the width and height of the ImageView and pass these as parameters for the bitmap generation. This way, the image will be fitting perfectly within its container. Since I am using a home screen widget, this is not possible, as far as I am aware, though. As a result of this, I am trying to derive the size of the ImageView by trying to figure out what size would the text fields have, but I also have to calculate those and so far have had no luck with getting it right. What I end up with is always having excess space above and beneath the image, or on its sides and the size of this is really varying according to the screen the widget is presented on. You can see some examples in the two links below.
Example A This is the default widget size. For this screen, only a small gap can be seen on the sides of the graph.
Example B Resizing the widget and recalculating the size leads to having bigger gaps on the two sides, though. For other screens the issue can be even more jarring.
My question is:
How could I get/calculate the necessary resolution for my bitmap?
I'm new to android programming and I was wondering about how to get a background image (ImageView) show a drawable with a correct size.
Let me explain well, let's imagine we have this image:
I want this image to get shown with this aspect ratio on all screens.
Since I don't want the image to get stretched, I would like to set manually how my image gets shown, maybe by modifying its scale with thirdy part software.
the problem with this image is that, in portrait mode, it's not guaranteed to see the ball, and so I want to crop the part of my image so that the ball gets shown on the bottom-right edge.
I know that I have to import my drawable for all resolutions, but simply importing does not help... Any suggestions?
EDIT:
I'm facing this problem with dynamic background ImageView, so I'm not going to user xml layout files.
Using scaleType attribute doesn't help for this, or at least not just by setting its value.
I am porting a Java game to Android and the graphics in this game will not render correctly if the dimensions are not a square. Thus, the screen will not fit exactly onto my device. Is there anyway to stretch a bitmap, which contains the drawings, like an image? I know there is Bitmap.createScaledBitmap(), however this changes the pixel dimensions and thus the view will not render correctly. I want the dimensions to be the same, but the image should fill the whole screen. I can deal with distortions.
I have an iOS app that uses unique textured backgrounds (think of a game title screen or something) on each of the screens that I am trying to port to Android. On iOS they knew the resolutions and just designed the backgrounds around those. Obviously that isn't possible on Android.
What is the best way to put a textured/non-repeatable background into Android that works on various screen sizes and aspect ratios?
The solutions I have thought of so far are:
Fit the shortest dimension and allow the image to go off the edge of the screen for the larger dimension
Make the background image large and center it, then make it stretch out beyond the edges when the resolution is smaller than the image
Stretch the image to just fit (this is ugly and I'd like to avoid it)
Create a different version of the image for each resolution. (This seems way beyond the scope of what is possible for this project.)
As far as I can tell in Android, 1. and 2. aren't possible out of the box because background images set the size of the view to match their size (you can't tell it to just extend beyond the edges, please correct me if I am wrong).
What solutions would you use in this situation?
Use an (match_parent) ImageView to simulate back ground image. (By adjusting the scale type of imageview you can get the result of point 1,2)
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Use 9-patch image. You can specify which part of your image can be stretched. For your case I suggest adding a small "margin" to your image, and make it stretchable using 9-patch. Only the margin will be stretched, maintaining the aspect ratio of your image.
android developer 9-patch
Create a different version of the image for each resolution.
As you're probably aware, Android can run on a wide variety of devices with varying resolutions and screen densities. To be fully compatible with all of them takes a bit of graphics work; you need resources for each possible screen density (and in this case, resolution).
For full details, see Supporting Multiple Screens in the Android developer documentation.
Unless something has gone seriously wrong in your development process, you should have the original files (e.g. .psd) to work with when creating these resources.
Looking for advice with the next problem. I am developing small game and I have a *.png file for background. 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. 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:
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. It will just make the size of *.apk bigger. And if I will use the methods to set background it will stretch the image - not a good idea I think.
In the onDraw() method draw the image on canvas. 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 .
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.
see this link.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch