So right now im mkaing a game(android) and ive got a couple questions. Okay so whenever i draw on the screen any image that is not 320x480 it will fill only a section of the screen, for example if a draw a image that is 500x500 it will only show the top left part of the image, and if i draw a 32x32 it will fill only a part of the screen. This is a problem though because in order to get a sharp image i need the image to be larger in terms of pixels or else the image comes out pixelated. Basically is there a way to compress a large image into a set area, make a 500x500 image fit into 320x480 area without it cutting the image.
Usually loading scaled down version of Bitmap files is preferred as shown here in Android documentation, using a scale option.
However from your question, I presume you'd like to read just a rectangular part of the image file. Luckily, there is a class BitmapRegionDecoder
which does just that:
BitmapRegionDecoder brd = BitmapRegionDecoder.newInstance(<file_name>,true);
Bitmap b = brd.decodeRegion(new Rect(0,0,32,32), BitmapFactory.Options());
//-- In the Options object above, you can set scale,
//-- which will read scaled down region and do it so faster--
//-- after you no longer need above resources anywhere, remember to free them --
brd.recycle();
Put the following size images in the folders:
Hdpi: 480*800
Ldpi: 320*460
mdpi: 720*960
xdpi: 800*1280
Related
Can I use just one png image big enough for all densities? Or do I need to create one png image for every density?
I tries placing just one png image in drawable folder but it becomes blurry. So I placed that same image in the drawable-xxhdpi (my test device density) and it shows correctly (clear). I guess that the blurry effect is because it is multiplying for the scale factor of the screen density. The idea should be to say to Android to take the big image (xxxhdpi) and make it smaller (never bigger).
how to use vector images which scale up and down according to the screen sizes?
I m using a vector image in my project but it is not scaling in big screens.
what you mean by being scaled ? maybe you are misunderstanding.
if you mean the image size would be changed in various screen sizes such thing wont happen. the image size is what you provide in your imageview (or any otherview using the image).
the only different between vector assets and normal asset is that vector asset are redrawn every time so you wont have a low quality image no matter how big it is and which device it is being shown in. so you wont need to save various size of image for various size of screens.
if you want to change the size of image just change the imageView height and width. you can use dp (density independant) unit.
you dont have to do anything for support vector images for diffrent screens its already scale for the relevant dp, Can you give more details about your problem when you say "it is not scaling in big screens?"
maybe you want the image on tablets will be bigger?
I calculate inSampleSize to use Bitmap decode method to resize large png files.
When creating a this new bitmap and Log info it's width and height with .getWidth() and .getHeight(), its pixels count has grown by 3 relative to its original size.
code: from line 121-204
https://github.com/abisai1221/android-bitmaps/blob/master/png%20to%20bmp
all help is greatly appreciated.
I would bet on screen density being applied. When you load bitmaps from resources, you have to put them in right drawable folders, because Android will automatically scale them to match your phone's screen density. Each folder should contain only images for one specific density. For example, if you have a high density screen in your phone, you should put images in drawable-hdpi folder to get them without scaling. If you're getting 3 times larger images, you probably have a very dense screen, like Nexus 5 or recent Galaxy S phone. To disable scaling, put images in drawable-nodpi folder.
I saw another post of resizing issues and came across the ".inScaled" field of BitmapFactory.options.
This executes by default to scale bitmap to target density.
I set this field to false to prevent it from executing and the images come out perfectly, tho Im still confused as to why it scales the image by a factor of 3.
I am building an android application and I have issues with images.
I use this image:
and here's my result:
As you can see, the first image is pretty clear but not on my android application. The image is blurry.
I tried using an ImageView and ImageButton but same result for both.
Any solution to this?
Why the image quality is blurry?
Simply because the image size and DPI is low. If you use a small images on a large device, the low resolution image will have to expand to the size in the ImageView or ImageButton property (layout_width and layout_height).
Let's pretend we need to use a small device phone like the Nexus One. This has a resolution of 480 pixels by 800 pixels.
If you have an image that is 128 pixels by 128 pixels within 100% scale, you will put 3 images side by side.
Let me demonstrate you with this example. I am using two images:
https://www.iconfinder.com/icons/310718/fiddle_instrument_music_violin_icon#size=128
https://www.iconfinder.com/icons/310718/fiddle_instrument_music_violin_icon#size=512
Here's the test:
Left Side
I use the 128x128 first, then the 512x512 using the layout_width and layout_height with 128xp as the value. (This is not recommended, just for test purposes).
As you can see, they are very similar in quality. Which make sense since both are sized at 128x128 pixels.
Right side
I used the same 128x128 first, then the 512x512 using the layout_width and layout_height with 130dp as the value. Now you can see the difference in quality (look at the strings). With high density pixels, the 128x128 will have to be stretched and the 512x512 will have to be shrunk. This way, the 128x128 will lose quality image and makes it blurry.
Conclusion
Make sure the images you use have a higher resolution and a high DPI and make sure you put them in their respective folders. (drawable-hdpi, drawable-ldpi,
drawable-mdpi, drawable-xhdpi,drawable-xxhdpi).
Perhaps you're inadvertently upscaling it. Try adding android:scaleType="center" to your ImageView's XML code. If it's then sharp, but not displaying at the size you need, you'll need a higher resolution source.
Convert the images to PNG i think have better quality in that picture. And check the size
I have a an image of a text field that I want to put as the background of my text field. I made a nine slice image so I can change the size of the image but retain the integrity of the corners. I have this image in my drawable xhdpi and I want to use the same image in my hdpi and mdpi. I need to do this for multiple images in my app so I do not want to have to make a nine slice image for each pixel density. When I drag my nine slice into photo shop to change the size of the image the black lines above and to the sid of the text box are now in the image, not a pixel above/left of my image. Is there any way that I can scale my nine slice? Or do I have to make a nine slice for each pixel density?
Rule of Thumb: When you scale your images for each density, watch out for aliasing problems even when the height and width are in integer multiples.
That is: make a nine slice for each pixel density to be safe as your black dots/lines may get cut out.
If you want to use the same image for all of them, just create a new folder in res called drawable-nodpi, and it'll use that same image for all densities with no scaling. That's what I do for the majority of my 9-patches if the borders are pretty thin. If you have borders intended to be thick on an xhdpi device, keep in mind they'll appear much thicker on an mdpi device if you use the same one. If that's not an issue, go for the nodpi approach, in my opinion.
Scaling an image with Photoshop will either remove or add pixels to the image. When it does that it will change the scale/fill guides so they are no longer 1 pixel wide and solid black. To avoid this crop your image to remove the guides before you scale the image then add them back in.
Or you can convert 9-patch
images to the other resolutions using this Google 9-patch tool.