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
Related
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.
I am newbi in android world and working on an android app.
I have used many of buttons and image buttons in my app.
Now i want to replace them with an background image. (So i can make nice gui easily)
I know i can use view listener and i can find the clicked x and y coordinate from onclick method.
But I want to how can i find which part of the image was clicked. Because different phones have different resolutions how can i do this efficiently ?
And one more thing, Is it good to handle gui this way ?
Thanks !
You first need to set the initial window width and height before execution of other code and load an image of quality based on the screen size. so for example you can have 3 sets of images of different size. One for 5 inch screens and below. Another for 7 inch tablets. Another for 10 and possibly another super high quality image for anything above. As many devices coming on in the near future are reaching resolutions much higher then previously developed for. Just run code initially that detects the initial window size at hand and load image based on resolution of that device. From their program as you plan to.
Also to jump on answer above. Detect rotation event and adjust image accordingly. and update accordingly. You are adding more programming of course but no reason what you are asking could not be done. Just a few extra events detection and you should be good to go to accomplish exactly what you are looking to do
AFAIK it is not good practice to handle the GUI this way. Mostly because android screen size is not standard and so you can never be sure that it will work perfectly in all the devices. You can ofcourse handle the events using onTouchListener and see which part of image was clicked from the coordinates in the MotionEvent Object. But, I would not recommend doing so.
Creating layouts of different screen sizes and handling the events using Buttons and ImageButtons will be good.
you can use android:background attribute for background images in the layout xml files...
however you also can set the background of UI element programatically
I am developing a 2d android strategy game, it runs on SurfaceView, so I can't(or can I?) use LibGdx's particle system. And I would like to make a raining effect, I am aiming for something like this( http://ridingwiththeriver.files.wordpress.com/2010/09/rain-fall-animation.gif ), I don't need the splash effect in the end (although that would be superb, but probably would take up a lot of system resources). How could I achieve that raining effect? Any ideas?
There's one similar code from JakeWharton called DroidFlake. It seem like snowfall effect but worthy to check DroidFlake
The easiest would be to put the rain drops in a PNG with alpha and display that at the end. You would only need a few frames to make it smooth, but it would eat up a bit of resources if this were a full screen image.
If you cannot afford this, you could put each distinct raindrop in a smaller PNG file with alpha, then display this (these) single PNG(s) multiple times. This would reduce the memory used a lot, although your rendering code will get a bit more complex, and depending on how many drops you have, it might affect performance.
A trade-off between the two would be to have a PNG file that contains more drops, but not for the full screen, and display that a few times.
You could have another frame (or frames) where the raindrop splashed, which you could display before ending the animation of each drop, or you could have the final frame in the large PNG contain the splashes.
There are many ways of doing this. You could create a sprite sheet like:
In that spritesheet, you could simply have a drop of water advance in each frame until it reaches the bottom and you create the splash effect. You then need to be able to animate it (very easy). You can then create multiple instances of the image, or draw the one image multiple times to create the rain storm.
I am developing a 2D game for android using lots of sprites. I am drawing on the screen using OpenGL ES. I separeted the sprites into several pictures (around 7). I read, that loading a picture into memory takes most of the time. Every picture contains a diffrent category of images: for example - background elements, enemy units, etc, etc. I load them in a defined order: background picture first, so background elements are drawn at the bottom, then the enemy units, etc. But what should I do, if I want to draw elements of the background at the top of the canvas. Should I load the picture with background elements twice? Or store that special element in both pictures - the background picture and the picture containg top elements. Or there is a better way? Thanks!
Loading has nothing to do with rendering. As long as you render something after you've loaded it, then you're fine. The order that things get rendered in is not affected by the order you loaded them in.
Now, if it's an issue of memory (ie: you can't load everything at once), then the steps you need to take depend greatly on exactly what you're doing. We can't really give you anything more than general advice, because we don't know everything about what you're doing.
In general, you need to break down your rendered world into chunks that fit in memory. This will put constraints on your design: you can't use stuff (be it backgrounds, enemy units, whatever) from outside of that chunk. Since you're developing on limited platforms, you're just going to have to put up with those constraints.
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