So I seem to be running a problem on an Android program I'm writing. I'm still a noob, so please help me out understanding the problem and how I can solve my issue.
I'm creating a simple game and I'm doing all of the graphic designs for it. My images are 1024x1024 or 1024x512 with a resolution of 300 pixels/inches. Yes, I understand that I'm probably over doing it. I just want the graphics to be really clear and to look good. All of the graphics are in the drawable folder not in hdpi, ldpi, mdpi, xhdpi or xxhdpi drawable folders. Now, the problem I'm having is that my app was working fine until I added one more 1024x1024 image to the drawable folder. Now the app just crashes as it tries to load an image. The problem I get is memory over flow. It doesn't matter what activity is running, it seems that once it hits a limit of loading images it crashes. I read the android resolution page http://developer.android.com/guide/practices/screens_support.html but maybe my English is not good enough to understand.
Should I resize the images to each resolution (hdpi, ldpi, mdpi, xhdpi and xxhdpi)?
When I move from one activity to another activity in my app, do I have to clear the ImageViews in the previous activity to clear up memory?
Please help me understand how this work.
Thanks
David
Ironically, the method you're using is probably making your images look worse.
When you place raster images in the root drawable folder, they are assumed to be mdpi, and will be scaled up for the other densities. This means that your 1024x1024 bitmap is scaled up and pulled in as a 2048x2048 bitmap (which in most cases is far larger than you probably need). So yes, your best bet is to make the images whatever size they need to be for a particular density, and create separate images in each drawable folder that you need to support.
Also to note, it doesn't matter what resolution (you mentioned 300 ppi) you set your images to, the only thing that matters is the pixel dimensions.
I think you got vm budget out of error.I faced this problem while working on my previous application. After searching i found solution from developers guide ... fallow this link hope it solves your problem.Thanks
Related
Until now, I did not pay too much attention to how to store drawable resources.
I usually generate multiple versions of an icon and store them under the drawable-mdpi, drawable-hdpi, drawable-xhdpi, ... folders.
For other images for which I don't have multiple versions, I inconsistently store them under drawable-nodpi or drawable.
However, I recently encountered an issue related that bring all my attention to that. I stored a 100KB image under the drawable folder. However, my app was regularly crashing, stating it could not allocate 18MB!
After some searches, the reason was that the image was scaled to fit the screen resolution and it resulted in a way heavier image. The fix was to move it under the drawable-nodpi folder which prevents that scaling.
So now, I am trying to better understand where I should locate my images and how this scaling effect works to optimize my app on that part.
I have done plenty of searches, but resources are limited or unclear on that subject and the official documentation kinda really sucks.
I am aware of the official explanation for the drawable or drawable-nodpi, but it does not clarify everything.
Typically:
How does the scaling work? Let's say I have res/drawable-mdpi/image.png. Does that mean the image is scaled if I have a screen different from mdpi, or will it also be scaled on mdpi screens resulting in possibly heavier image size on every device?
Following previous question, if the image is not scaled for mdpi screens but scaled for any others, then does that mean that if I provide a version of this image for every screen density, the scale will never happen? Or at the opposite, if the image is scaled also on mdpi devices, then having different version of the same image for each screen density will still scale the image, but using the version of the image matching the screen density of the device?
How to deal with icons for which I do not have multiple versions? I am afraid that if I put this single version in mdpi, it just scales it and uses so much more memory than necessary. In such case, should I put any icon for which I do not have different versions under nodpi?
On the other hand, if whenever you put an image in mdpi, hdpi, ... it scales it even on devices matching the density, then should I just move everything under nodpi or some high resolution like xxxhdpi where it can only be downscaled?
Thanks
Let's say I have res/drawable-mdpi/image.png. Does that mean the image is scaled if I have a screen different from mdpi
Yes.
or will it also be scaled on mdpi screens resulting in possibly heavier image size on every device?
No.
if I provide a version of this image for every screen density, the scale will never happen?
Yes. Your APK will be larger, due to the 7 copies of the drawable.
How to deal with icons for which I do not have multiple versions?
Option #1: Decide what density that particular version belongs in, and put it there. Android will upsample or downsample the image for devices operating at other densities.
Option #2: Put it in -anydpi or -nodpi, in which case Android will not upsample or downsample it. However, in this case, you need to be controlling the size of the image yourself (e.g., in the ImageView).
Option #3: Replace the icon with one that either you have all relevant densities or one that is an SVG that works as a vector drawable when imported using the Vector Asset Wizard in Android Studio.
The decision-making needs to be based both on memory consumption and what the result looks like. A low-memory solution that looks awful is unlikely to be a good choice.
should I just move everything under nodpi
Probably not.
or some high resolution like xxxhdpi where it can only be downscaled?
Probably not. It is unlikely that a massively downscaled version of your icon will look very good.
I asked a question yesterday.
Why do resolutions become smaller after a image file be read by program?
Very thanks the people helped me.
They let me know that if I use the devices having different densities, the resolutions of the same image may be changed.
For example:
I have a image with resolution 1440x2560 and a device with density 3.5.
The revolution will become 1260x2240 after the image be read into the program.
But I don't want the resolutions of the images to be changed, because the proportion of the images on the screen will be different.
I tried to find a solution, but I failed.
Is there a convenient way to fix the same resolution of the same image on the different devices with different densities?
If you want your images to be adjusted by Android, I understand they are drawables even if you didn't explicitly mention it, add them to the drawable-nodpi directory.
As far as I googled about it, in general, you provide several images with different scales and put each image into folders like ldpi, mdpi, hdpi and so on.
The question is if there are any problems with that I provide a image with only one scale which is large enough.
Using 'dip' it resizes the images smaller for several device resolutions.
Hope my question makes sense.
No, as long as it is a vector image you should be fine
if you using bitmap image yes you should provide several image with different resolution but if you have no problem with bad quality image when it forced to be streched to big resolution no you didn't need to provide it.
for showing the image in the device I prefer measuring the device size (width and height)
then scale it programatically
I think this should be enough, but if you have some other question about this topic feel free to ask in the comment :)
Very much a newbie question here but I cannot find a clear answer on the net or in the books I have. I am designing an app to be compatible with all sizes of android devices and I want to display a images in my app. I understand that android automatically scales images to suit the resolution of the screen being displayed.
My question is what do I look for in the images I use? E.g. I have a picture that's 2418 x 2192 # 240dpi. As far as I can tell, this should be perfect for a larger screen such as a tablet. If I put this into the xhdpi image folder in my project will it be detected by other devices and scaled to suit their dimensions/resolutions? Do I have to create 3 different versions of this image in photoshop and put it into the relevant dpi folders?
I can't find a nice beginners tutorial for this stuff so haven't a clue what I'm doing!
This is the best place to start for multiple screen size support:
http://developer.android.com/guide/practices/screens_support.html
The short answer is, yes, you place the images in the different directories based on resolution and Android will select which image to use based on the device's properties. You should make a smaller set of images, for example, for mdpi and hdpi. You don't "have to" do that, and if you just place the image into drawable it can be scaled down, but that's generally not the best way to go.
This is something you'll deal with on just about all project, because there simply are so many Android devices. This resource has helped me greatly.
If you have a resource in only ONE folder, Android will scale it for you. The different drawable folders are intended to allow the developer to provide properly scaled images with minimal effort. For example, you'll want to provide an xhdpi in addition to the "standard" mdpi image to make assets look better on high-resolution devices. The app would work fine (and have scaled images) in all resolutions, even if you provided only a single image. However, if you provided a xhdpi drawable, then there's a bit of overhead to scale all of those down, and especially for icons, the results may not look very good (or even be recognizable).
I generally provide assets for mdpi and xhdpi, but if the app will see frequent use on low-res devices, I provide the ldpi as well. If possible, I include all four.
Note: The image you mention is much too large to be included in the UI resources and would probably best be placed in assets and loaded on request. Even on xhdpi devices, it would have to be scaled down.
I was wondering if there is a way for using one set of images for all the resolutions. I have an application, and I want to port it on all the android resolutions but I don't want to store 3 sets of images, for every resolution category. What would be the best practice to use as less images as possible.
Thank you!
Sure, you can provide only one set of images, in the base drawable directory, and Android will automatically resize as required.
This may result in aliasing and images that are not as clear as would be possible if you were to provide images sized properly.
You probably mean density, not resolution (in Android, applications do not work directly with resolution).
Actually, you don't need to support all 3 densities. Quote from developer.android.com:
This pre-scaling mechanism works independently of the source. For instance, an application targeted for a high-density screen may have bitmaps only in the res/drawable-hdpi/ directory. If one of the bitmaps is a 240x240 icon and is loaded on a medium-density screen, the resulting bitmap will measure 160x160.
I've done simple test: removed ldpi and mdpi density images, launched my app on mdpi skin and it scaled hdpi images nicely. It's not the best practice, but android's prescaling does its part as described.
Please read this for more details.
I wrote a tool which makes this all a lot easier to manage. Although the images are stored in separate directories - the tool shows all resolutions together, and lets you auto-scale from one to another.
you can get it here: http://hobbyistsoftware.com/AndroidImageViewer