I think I've got quiet a common problem in regards to Android development, but cant seem to find the answer I'm looking for.
If I make a canvas on Photoshop 800x480px (240ppi) and make a logo within that canvas that is 282 x 121px, I cant understand why when I display the image it takes up 3/4 of the screen in my emulator with the same 800x480px.
The code I use to display the logo is the following
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
//in the onDraw
canvas.drawBitmap(logo, 0, 0, null);
Thanks in advance!
Possible Solution
Not sure if this is the best way to do things and would appriciate any feedback, I came across this solution (typically when you've already posted a question :D ) was to code the image as follows:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
//Load images
logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo, options);
The image was probably only taking up 3/4 of the screen in the emulator because of the pre-scaling android sometimes does.
Check out this article on how to handle multiple resolutions and screen sizes.
You could try placing the drawable into a 'res/drawable-mdpi/' directory or a 'res/drawable-ldpi/' to differentiate between medium density and low density screens. Another option is to place it in a 'res/drawable-nodpi/' directory to prevent any pre-scaling. I've even placed some images in the 'res/raw' folder to get the same effect.
However, if your current method works, go for it!
Related
I use transparent png image for my app, but when app runs the image loses its quality and it is not exactly same, its kind of distorted also blurred. Is there something that i can do, like bitmap options?
mBitmap = BitmapFactory.decodeResource(res,R.drawable.img1);
I had this problem too. I did solve it using another format than png (in my case jpg was enough). If you still want to use an alpha channel your only remaining choice is gif, even if this wouldn't be the best choice normally.
Could it be the screen pixel density does not match that of your Bitmap? Unless you specify otherwise, your Bitmap is assumed to be at 160dpi, so it will be rescaled as necessary, depending on the device, when you load it.
You can have different versions of your Bitmap, designed for different pixel densities. Just like app icons, these go into the appropriate res/drawable-*dpi/ subdirectories.
I had the same issue when trying to process pictures from android camera.
I solved using this code:
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDensity = 96;
Bitmap originalPicture = BitmapFactory.decodeFile(pictureFile.getPath(), options );
It seems this problem is related to the RGB format of your image and the dither option.
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),R.raw.book11);
Canvas c = new Canvas(icon);
using this line of code for making canvas actually increases the size of the canvas(150x177) from the bitmap(100x118). If I use predefining the size of canvas, the bitmap gets cropped. Help please.
thanks.
I solved mine with this equation:
event.getX()*3/2;
event.getY()*3/2;
the system will automatically up-scale your image asset when you use it for a 'larger/higher' screen resolution/density if that particular asset is missing for that particular configuration (i.e. you're referencing an image on a hdpi device while that image is not present in the drawable-hdpi but only in raw drawable directory).
Note that scaling factor depends on device's screen density. In your case that seems to be 1.5.
EDIT: If not done yet, please check the link.
What's the best image file format for Android in terms of memory? PNG is recommended for iOS as xCode does some magic with it.. Is it the same for Android?
I'm currently developing a big app with multiple animations going on (sliding in screens, fading etc etc). All works well so far! However I have noticed the view animation where the view contains an ImageView with a (quite large) PNG as the source is a bit laggy.
Obviously I can make the PNG smaller, but is there anything extra I can do to reduce the amount of memory the ImageView takes up/makes the animation smooth? I know PNG has a much larger file size than JPEG, but I can't see this being a problem, the JPEG or PNG (I assume) is eventually stored as an array of colours, so they would both take up the same memory. PNG is probably better for loading due to less cycles uncompressing. Again I only assume, my knowledge of image file formats is null.
Alternatively is there anything else causing the lag? Is the bitmap scaled to fit the view each onDraw() during the animation so should I scale the bitmap in code before giving it to the ImageView?
Thanks,
The formats supported by Android are: PNG, JPG and GIF (also 9.png).
The recomendated is PNG as said in dev guide
All of them are stored in memory as a Bitmap, so the most important thing is the color deph, like this:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
More info: stackoverflow
and add (after honeycomb):
<application
android:largeHeap="true"
...
to your manifest file :=)
thanks to my dear friend :)
I'm having trouble with loading textures regarding their resolution on openGL for Android. If the texture is 256x256 everything works perfectly, but if it's other resolution, the program throws this exception on start:
android.content.res.Resources$NotFoundException: Resource ID #0x........
I found a code on the internet that changes the density of the bitmap this way:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDensity = 240;
and by doing this, I can load 512x512 textures. But I'm not able to load for example 128x128 bitmaps, because I don't know which density I have to use. I'm not sure either that this is the normal procedure to load textures, because I don't found many information on the internet.
Thank you for reading!
You dont need to specify the density, you just have to make sure the image is a power of 2, which you seem to have done. I would leave out the density option and just specify Config.ARGB_8888;
If this doesnt fix the problem can you show your code for loading the texture
If anyone has this problem, or the textures show up messed up (like ones being another, or other weird things) I was able to solve it by deleting all the items on the "drawable" folder and putting them in again. It seems the pointers to the images were corrupted or something.
I created a background .png for my application in GIMP. It's resolution is 640x480, which from googling, seems to be the resolution for a default emulator. My problem is when I apply the background to the RelativeLayout with android:background=#drawable/bg and run it, there are lots of artifacts in the image. As if the emulator could not provide enough colors to display the .png correctly. What is going on here?
P.S. This image is nothing to fancy, just simple lines and radial gradients.
It's resolution is 640x480, which from
googling, seems to be the resolution
for a default emulator
640x480 is not even an officially-supported resolution in Android, let alone a "default" one. Here is the list of supported resolutions.
Also, you want to watch your color depth. I forget the details, but not everything can necessarily handle 24-bit color, due to LCD limitations.
i've gathered 3 possible solutions:
for each problematic image that you have, create and use a bitmap drawable in the xml.
the disadvantage is that you create multiple files. i've tested the other special flags (including the code changes), and haven't noticed any difference.
res/drawable/image_file.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/problematic_image" />
put the problematic image in the drawable-nodpi folder. the disadvantage is that it will use more RAM this way, as it doesn't downscale according to the density. a similar approach can be done in code:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inDensity = 1;
options.inSampleSize = 1;
options.inTargetDensity = 1;
options.inJustDecodeBounds = false;
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.problematic_image, options);
imageView.setImageBitmap(bitmap);
if the file is a png file, take a pixel that is least noticable (for example the bottom-left), and change its opacity to 254 instead of 255.
the disadvantage of this method is that it makes the image to take more space , and change the image itself.
all methods worked on galaxy s and galaxy s2 , with android 2.3.x .