How to stretch a bitmap? - android

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.

Related

How does PreviewView fill-scaling handle images which are higher resolution than the view?

When dealing with the PreviewView "FILL" scaling types (e.g. PreviewView.ScaleType.FILL_CENTER), the docs seem to imply that the image will not be scaled if it is higher resolution than the view.
Scale the preview, maintaining the source aspect ratio, so it fills the entire PreviewView, and center it in the view
In cases where the image is lower resolution than the view, I can understand why the image is scaled, as otherwise there would have to be black borders (i.e. would not fill the entire view), which FILL is guaranteeing will not be displayed. In cases where the image is higher resolution than the view, the preview image already fills the entire view (i.e. doesn't need to show black borders) so my assumption would be that the preview does not need to be scaled.
For example, your image is 1920x1080 and your view is 800x800, I would expect PreviewView to not scale the image and just crop an 800x800 square out of the centre of the existing image (using FILL_CENTER).
However when looking into the code I could not find any condition which actually stops scaling from happening when the resolution is higher.
For example:
preview.setScaleX(surfaceRectInPreviewView.width() / mResolution.getWidth());
preview.setScaleY(surfaceRectInPreviewView.height() / mResolution.getHeight());
preview.setTranslationX(surfaceRectInPreviewView.left - preview.getLeft());
preview.setTranslationY(surfaceRectInPreviewView.top - preview.getTop());
This code is after all the matrix transformations are done (which I don't fully understand), so I may be misinterpreting it. So I just wanted to validate my assumption about whether images are scaled if they are higher resolution than the view.
PreviewView is designed to display the largest possible FOV. If you wish to display a smaller FOV, you can take a look at the zooming APIs: https://developer.android.com/reference/androidx/camera/core/CameraControl#setZoomRatio(float)
The scaling in PreviewView is for getting rid of the black bars in case the aspect ratio of the preview does not match the aspect ratio of the view. It doesn't matter what resolutions they have.
In your example, the output image is 1920x1080 and the view is 800x800. For the FILL_* types, the output image will be scaled to fill the view, in which case it will be scaled to 1422x800, then the extras will be cropped out. For the FIT_* types, the output image will be scaled to fit the view, in which case it will be scaled to 800x450, and placed inside of the view.
Hope this helps. Please let me know if you have more questions.

Python game on android image does not fill screen size

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.

Android resource that scales on one axis and tiles on another

We're working on the Android version of one of our games and we're having a problem with a background image. On iOS, it's really easy to scale the image down to the appropriate width and configure it to stretch vertically.
However, on Android, we can't figure out a way to do this. Basically, because of the myriad screen sizes and resolutions, we'd like to have our background image (which frames the play area) scale down to fit the width of the screen. Then, the bottom and top portions of the image should have the right aspect ratio but the middle part should tile (or stretch even) to fill the rest of the area.
We tried this with a nine-patch file but nine-patches don't scale down. We've tried it with LayerDrawables which works well except that it won't tile the middle as the size of the layout changes; it just stretches the entire image instead.
Is there any way to get a drawable to scale to fit horizontally and then tile a section to grow vertically?

Padding out a bitmap to make it a power of two

I have a bunch of images of dimension 480x800. The intention is that these will fill the entire screen of the device.
I would like to take the image and use createScaledBitmap() to resize the image to fit the screen exactly. I then want to put this bitmap inside another bitmap (not stretch it), which will have dimensions that are a power of two.
So for example, if the screen size is 320x480 I resize my 480x800 image to be 320x480. I then want to place this 320x480 image inside of a bitmap that is 512x512 so that the original image is nested in the top left corner (with the extra space being blank).
How can I achieve this on android? I have gotten so far as to resize the image to fit the screen exactly, but not making the power of two bitmap and filling it.
Solved this by using a texture atlas

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. 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

Categories

Resources