Android sprite moves when it is supposed to be stationary - android

I created a sprite that is 18 frames long and it is supposed to be stationary and run through the frames and loop the animation, but when it runs it moves slowly across the screen as it animates until it hits the last frame then jerks back to the original position where it was suppose to stay at.
I don't know the reason it does this and have tried to make it smaller sizes and larger sizes, at some sizes it does not move but just animates likes its supposed to do.
If anyone has an answer to why this happens and how to stop it from moving and stay where it is supposed to be it would be greatly appreciated.
I use only 1 row for the sprite and I used gimp to create the sprite and saved it as a png.

The code is rather long thats why I didn't add it but it based on this tutorial
aquarium live wallpaper tutorial
I think I found my answer though, I had the png sprite in a folder I created named drawable, where I put all the images, I tried it again but put the sprite in the other folders this time and it didn't move, I tried scaling the sprite also which made it go a bit wonky, I was hoping on using scaling instead so it stayed the same size for all screens, this works for normal images but for sprites it causes some issues, maybe I'll try that with the largest size in the drawable-large-mdpi folder and see if it causes any issues that way.
I'll update with that method and let you know.
It would be nice to have just one image and scale it instead according to screen size.
Okay I found the answer finally and it might help those creating sprites,the width when divided into the amount frames has to be a whole number since the measurements are in int not float or double, so it moves because when the frame gets divided it came out to a fraction instead which caused it to moves (I guess) whatever that float extra measurement was.
Example: if the sprite was 100 pixels high and 1082 px wide and I had 15 frames the width of each frame would then be 72.133333333px wide for each frame causing an error that would move the image that extra length while keeping the animation looking nice. now if it was 1080px wide each frame would then be 72px wide a nice whole number and then would animate perfectly.
So for those creating there own sprites make sure the wide equals out to be a whole number when the frames get divided (My problem was I was creating one image scaling it down and not checking the width of each frame so only one image size usually work, sometimes I got lucky and got two, I should of figured this out because it all runs in int not float for measuring the width.
Hope this helps out someone else making sprites.
Sam

Related

libgdx -> camera and viewports

I have made my game, which scaled accordingly to a % of the screen width with the Gdx.graphics.getWidth() method, and it works perfectly on all screens.
Now I am trying to learn viewports, but seems like there is a lot more trouble, like when does it take in world coordinated or when does it take actual screen pixels.
Do I have to constantly convert beetwen these two measurments? It seems like there is alot more trouble, than if I just scale it the old fasion way...
I can use the whole screen, and manually make the pictures non streched, if I used a FitViewport I would have like blackbars and the game would be totally different.
Any clear suggestions to why to use these ports, cause I cannot seem to understand them...
The thing is that you do not have to do anything especially making any conversion.
Viewport is a kind of tool that handling your app's rendering on many types of screens (I mean many other ratios) and you do not have to worry about it anymore.
Only thing you have to do is to "tell" viewport what is the size of your screen and to handle screen resizing by updateing the viewport. Then you are treating your app like it would be always for example 800 x 600px no matter how it looks actually.
The way your app will render depends on what viewport implementation you will use. For example:
FitViewport will fit your screen to device and add some black bars
FillViewport will fit your screen to device and cut off overflowing part
and so on...
The a look at official Viewport libGDX tutorial. Also you can take a look at this thread to get some information how to deal with viewports.

Android: Doing all my drawing on a SurfaceView; what's the proper way of making sure my drawing conforms to any screen size?

I'm making an app (a game, to be exact) where each activity uses a SurfaceView for the UI. All of the drawing is done through onDraw. I am also designing this to use no Bitmap assets, so everything that is drawn is produced directly by the app. I'm trying to design the app in such a way that it can easily be viewed on any screen size.
So here's how I'm accomplishing this: I'm doing my testing on a Galaxy S4, which has a screen size of 1080x1920. In the constructor for each activity, the width and height of the current screen are calculated and stored as ints "w" and "h" (the app is already locked in portrait). Then, whenever anything needs to be drawn onto the screen, I multiply the desired dimension (as seen on my 1080x1920 screen) by either w or h, and then divide by 1080 or 1920. Since I'm not using any Bitmap assets, I never need to worry about pixelated images or anything this way.
This gets the job done, but seems like a bit of a roundabout way of doing it. I figured there would be a better framework for getting this done, and I'm worried that these big calculations are eating into my drawing time (running at 30FPS can get a little jerky).
Is this is the customary way of doing it, or is there a better way out there?
There's a very simple yet effective way to do it, you can declare all your sizes in a dimen file for each specific density/size, as you usually do for layouts e.g:
values/dimens.xml <--- With default sizes
values-sw600dp/dimens.xml <-- Tablets sizes
(etc...)
Now before you start drawing, load all the values in your program only once, maybe onCreate of your drawing activity, using the following command:
float someItemSize = Context.getResources().getDimension(R.dimen.some_itemSize)
etc...
That way, you let the operating system do the pixels conversion for you, you should do it only once and most important, this will give alot of flexibility to your code because you will be able to change sizes from xml files without even touching your actual code, hence, the customization should be easier as well as future changes...
Hope it helps!
Regards!
There are two considerations: screen size and screen aspect ratio. If you simply scale everything to match the display, you will appear stretched a bit if you put a 4:3 device next to a 16:9 device.
You generally have two options for dealing with the aspect ratio: you can letterbox / pillarbox the output (which is fine for movies, but looks kinda lame for an app), or you can recognize that your output isn't always proportionately the same, and adjust the layout to fit nicely on the screen.
As far as size matching goes, when using a SurfaceView you can actually use a single size and then let the hardware scaler handle the rest. For an example of this, see the "Hardware scaler exerciser" in Grafika. I put a demo video here, though it's a bit hard to evaluate after getting rinsed through screenrecord + youtube. What you're seeing is the same couple of shapes rendered onto a Surface whose size has been set to an arbitrary value, and then scaled up to match the display. This is most obvious on the final setting, where it's drawing on a 106x64 surface.
With this, you could size your assets for 720p and always draw them the same way, and let the scaler deal with the stretching. See also this blog post.

What to do when an imagesheet is too wide?

I'm trying to develop for Android, using Lua and Corona Simulator. The maximum image size that Android accepts is 2048x2048 pixels. I currently have an imagesheet that's 148 pixels high and 4690 pixels wide. The sheet consists of 35 images on a single line. Is there a way I can break this imagesheet up in multiple lines of frames and still use the Simple animation method listed here (http://docs.coronalabs.com/api/library/graphics/newImageSheet.html) rather than the complex one, or do I really have to declare each frame individually?
I have tried scaling the image down to a width of 1995 pixels, and then scaling it back up inside the program, but this (obviously) resulted in terrible resolution. I also tried (although this was with another image that presented the same issue) breaking the image up into two lines, so it was twice as high and half as wide, but I couldn't get this to work with the Simple method in Corona I mentioned above. The Complex method seems like a last resort, because it's a ton of work and code declaring each frame in the imagesheet individually by x, y, width and height.
The sequence navigates your texture image from left to right, top to bottom. So use a 5x7 grid of sub-images (5 rows, 7 columns, instead of 1 row, 35 columns), sequenced in this manner. Your image would then be 740 x 938.
Alternately, you could have multiple image sheets and switch between them as required.

Drawable resource file max dimension

I've created a single sprite sheet for my game app, with each frame 100x100 px.
Everything's been working well until after I added a few more rows to the sprite sheet, then suddenly nothing gets drawn onto the screen any more. I reverted the sprite sheet back to an earlier version, which had less rows, and everything works again.
The sprite that works is 400w*900h px, and the one that stopped working is 400w*1400h px... I suppose there's a middle ground somewhere I just haven't bothered to look for it.
My question is what is there a dimension limitation on the resource image files? And is there another way to get around what I just descripted other than splitting it into different files?
Thanks in advance
edit: getMaximumBitmapHeight() helped a little: On my testing device (nexus galaxy) it's only 2000 something... And on the emulator is in the 30k range :S Is there a way to set the allowed size? And if anyone knows... how different is it from phone to phone
The Canvas class has a method getMaximumBitmapHeight() (& width , naturally ) to give you this information for a given device, you can just print it in a log message.
On possible work-around is adding columns. Your image is only 400px wide but 1400 in height. It could be that 1400 is too wide but I very much doubt 400px is too wide.

Android LIVE Wallpaper approach?

I'm entirely new to Android development, and I'm interested in making a live wallpaper. I was thinking about looping a set of pictures instead of drawing the animation. Is this a possible approach? If possible is this a suitable or ideal way of doing it, does it eat up memory and would i need images with different resolutions because of the fragmentation?(hundreds of different devices)
Thanks in advance :)
Sure, it should be fairly simple.
The main part of the Wallpaper engine is the Drawing of course. You need to handle the drawing manually, there is no easy way to loop through images... you need to code it. You will have to continuously monitor the time which has passed and adjust the displayed image based on that. You are given a Canvas object to draw to and it is all done during run-time in code.
The Wallpaper engine class has a event called onSurfaceChanged which gives you the width and height of the Surface which you will be drawing to. It will be called everytime the screen dimensions change (like if the phone is put into Landscape mode for instance). You need to have code that will handle any combination of width or height for all the device types out there. You will need to decide if you are going to stretch, center, tile or crop (Or a combination of those) the source images to fit any particular screen size. I would recommend scale to fit so that the image is either taller or wider than the screen and center the image either vertically or horizontally (cropping the extra bits) once that is done. I personally just use one source size and resize it to fit. You can opt to use several source files if you want which is the recommended approach I believe, but it's a little confusing.
I would start by creating a Live Wallpaper which just draws something basic like a shape to the screen. Then work out how to display an custom image and take it from there.
Good luck
You will need to be very careful about memory if taking a frame approach to animation--probably will not be possible to animate full frames--much more practical to move sprites on a background. See, fr'instance: http://davidjhinson.wordpress.com/2010/05/19/scarce-commodities-google-android-memory-and-bitmaps/
what if you have a png frame animation that is set at a certain x and y position that sits in a spot on the background image, both would have to be rescaled

Categories

Resources