Retrieve a Bitmap with a Fixed size - android

When you get a bitmap from a resource for example with:
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);
It is automatically scaled depending on the device's screen density. How can I avoid that and retrieve it in its original pixel size whatever the screen density is?
Thanks

What do you mean "It is automatically scaled"? When you call this method Android takes image from folder res/drawable-?dpi - depending on device where app is running. If your ldpi folder contains 30*30px image, mdpi contains 40*40px image and hdpi contains 60*60px, you will get bitmap one of this sizes(depends on device). Android does not change anything.
If you see that on the screen your image scaled somehow, check properties of ImageView.

Related

why when coverting PNG img to a Bitmap img in android studio, pixels multiply by 3.

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.

Setting ImageView divides Drawable size by two

I have created a dress up game app. After the user finishes dressing up his character, he/she can save it to the app's gallery.
I am using canvas and drawbitmap on canvas to generate the final image, which is then shown on the app's gallery. The xhdpi image size should be 600x900 pixels. When this image is shown in the normal android gallery, it is shown as 600x900 and it (got from info) looks big enough. However, when I show it in my app, it looks much smaller though the ImageView is set to wrap_content. Moreover, when dressing it up, it is also set to wrap_content and it looks bigger than the one in the app's gallery.
My theory is that because I'm trying to set it programmatically using
.setImageDrawable(Drawable.createFromPath(path));
and because the screen is xhdpi, it is dividing the size by two.
What can I do?
Likely, you're testing on a device that is not xhdpi, but hdpi or mdpi.
When you provide a graphic in a density bucket, Android will take care of resizing that image so that it remains the same physical dimension across all devices (that is to say, size in cm on the screen).
If you would like to use the raw drawable, you can provide the graphic in the res/drawable-nodpi folder.
If you would like to retain the density buckets, but your graphic is too small, provide the correct sizes in res/drawable-mdpi, res/drawable-hdpi and res/drawable-xhdpi.
I don't believe there is any way of preventing this behavior through Drawable.createFromPath(). You could load the bitmap directly using BitmapFactory and provide it into the ImageView using setImageBitmap():
File f = new File(path);
Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView.setImageBitmap(b);

How to put large png on screen

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

in which folder put image for setting to background of layout android

i am putting the image in three of the drawable,drawable-hdpi and drawable-ldpi for supporting all type of screen but when i see the output of background image in 240*320 screen resolution the background image is not as clear as origional so my question is where to, means in which folder i put the background image for supporting it all type of screen size and density, maens then my image should not distorted or bluer...
i am using the background image of size:320*480
thanks
make one folder name
drawable-nodpi
put your image in that and use it and dont keep that image in other folders.
the image will not be scaled or stretched.
Anoher Way
the best way is to make 3 different size images and put int drawable-ldpi(240X320),drawable-mdpi(320X480),drawable-hdpi(480X800) folder with the same name.
TO LEARN MORE PLEASE VISIT THIS LINK.
Supporting Multiple SCreens
You can put different same image in drawable-hdpi,mdpi and ldpi.Depending on the density of devices they will take the images from the corresponding folder.
With different devices you should have same image but with different size and density.for You can search in internet the specifications of devices.If density is less than 160 then put image in ldpi,if 160 then use mdpi and if 240 or above use hdpi.Also change the size of image to the screen size of corresponding device.Hope it will help.

Bitmap image from file does not scale on high density device

I have problem with displaying bitmap image on imageview on high density screen (480x800). When the bitmap image loaded from file on sdcard, the image does not scale to fit hdpi screen. On medium density screen it works normal (320x480).
public static Bitmap getBitmapFromFile(String src) {
File file = new File(src);
if (file.exists()) {
return BitmapFactory.decodeFile(src);
}
return null;
}
mImage.setImageBitmap(Util.getBitmapFromFile(filePath));
Screenshoot on hdpi & mdpi
http://202.148.2.34/~lorenz/iview.jpg
Try adding a scale type to it
mImage.setScaleType(ScaleType.FIT_XY);
There are lot of scaling type available. Check which one suits you.
This is an old question but I think there is an easier way to do this. It seems that when you load a bitmap from an external source (one not in your /res folder) the Android framework will treat it as if it was meant for the baseline device (ie mdpi). Therefore it looks great on a 320x480 mdpi screen, but not as much on an hdpi screen.
Now the system normally should scale for this on an hdpi screen by a factor of 1.5 (unless you supposedly you set a density in the bitmap object, but I don't really know how that works) but since you used the method setImageBitmap() the bitmap is not scaled, leading to your problem.
Supposedly if you use the method setImageDrawable() and wrap the bitmap in a Drawable the bitmap will be autoscaled just like you wanted.
I haven't tried this, but apparently this guy had the opposite problem of you.

Categories

Resources