Another bitmap's sizes - android

I added bitmap image to drawable folder with 64x64 sizes. Then I get a reference to this by Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.brick); The problem is in output I get an image with 128x128 sizes. I dont use any BitmapFactory.Options. Why it happens?

I added bitmap image to drawable folder with 64x64 sizes
I am going to assume that you mean this literally, and you put the images in res/drawable/. That is a synonym for res/drawable-mdpi/.
The problem is in output I get an image with 128x128 sizes.
That would occur if your device is an -xhdpi device. Android will resample the drawable from -mdpi (~160dpi) to -xhdpi (~320dpi).
Try moving your image to res/drawable-nodpi/.

Related

How to create BitmapDrawable from a file for different screen densities?

Well know that if we put an image 96x96 pixel in folder xxhdpi of project, it will be displayed in 32dp.
Now I have an image file 96x96 pixel, which is located on storage. I create a BitmapDrawable object from that image. All I want is: the image should be displayed in 32dp.
Here is what I tried:
BitmapDrawable bmd = (BitmapDrawable)BitmapDrawable.createFromPath(localFile.getAbsolutePath());
bmd.setTargetDensity(DENSITY_XXHIGH);
But it doesn't work. It looks smaller than 32dp. Can anybody show me how to do it?

Images in the 'drawable' folder are resized automatically?

I have a 1280x720px image in the res/drawable folder (not the drawable-hdpi, drawable-ldpi, etc. folder). But at runtime the size is 2560x1440px. How is this possible? Does android resize the images in the drawable folder?
Here is the code:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.start_image_left);
int h = bitmap.getHeight(), w = bitmap.getWidth();
I'm testing this on a Motorola Moto G gen2.
There's really no reason you should ever put a bitmap into the root drawable directory. It should typically be used for XML drawables only. Bitmaps in the drawable directory will essentially be handled as if they were in drawable-mdpi (scaled up proportionally for other densities).
If your goal is to have a bitmap image that is the same pixel size on all densities, you need to put it in drawable-nodpi. This will cause it to not be scaled.
You are correct, Android automatically scales your image based on the current device configuration. If however, you dont want that behaviour, you can create a bitmap resource file, put in the drawable folder and point it to the drawable you want to use like this
<bitmap xmlns:android="http://schemas.android.com/apk/res/android
android:src="#drawable/yourDrawableHere"
android:gravity="center" />
By specifying the center gravity, android will not scale your image no matter what.
To use this bitmap resource file, in your code, retrieve it like this
Drawable drawable = getResources().getDrawable(R.drawable.yourBitmaoResourceFileName);

setImageDrawable don't scale correctly with no resource images

I'm trying to load a image stored in filesystem into a imageButton. But it not scaling correctly. The file is the same (t700.png) in drawable resources and in filesystem, but the result is diferent.
This work fine:
button.setImageDrawable(getResources().getDrawable(R.drawable.t700));
This change scale / size and get bigger image:
String filePath = VideoActivity.this.getFilesDir()+File.separator+EPGApiService.LOGOS_DIR+File.separator+channel.getLogoFile();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
BitmapDrawable imgDrawable = new BitmapDrawable( this.getResources(),bitmap);
button.setImageDrawable(imgDrawable);
I need same result..
NOTE:
if I do
BitmapDrawable logoBitmapDrawableFromDisk = new BitmapDrawable(VideoActivity.this.getResources(),logoBitmap);
BitmapDrawable logoBitmapDrawableFromDisk2 = new BitmapDrawable(logoBitmap);
BitmapDrawable logoBitmapDrawableFromRes = (BitmapDrawable) getResources().getDrawable(R.drawable.t700);
The Drawables Bitmaps sizes are not the same!
logoBitmapDrawableFromDisk size is 410x123
logoBitmapDrawableFromDisk2 size is 205x62
logoBitmapDrawableFromRes size is 820x246
The real file is the same. I need the same size on this two ways.
BitmapDrawable(getResources(), bitmap)
Create drawable from a bitmap, setting initial target density based on the display metrics of the resources.
It looks bigger because it is using the resource object to adjust the image to your screen's density.
To avoid that use BitmapDrawable(bitmap)
Or resize your png to look right in a mdpi device.
Then BitmapDrawable(getResources(), bitmap) will give you the correct look in all devices.
The correct way to have same look in all devices is create different icons for each device density.
Start with the dimension for mdpi density. The dimensions for each the other densities should be calculated based in the following values:
hdpi multiply by 1.5
xhdpi multiply by 2
xxhdpi multiply by 3
Each icon should be put under separate folders with the appropriate name depending on density:
mdpi -> res/drawable_mdpi
hdpi -> res/drawable_hdpi
xhdpi -> res/drawable_xhdpi
xxhdpi -> res/drawable_xxhdpi
Set the ImageButton source in xml or use getResources().getDrawable(R.drawable.t700); the device will automatically select the right one.
For more information see this

Retrieve a Bitmap with a Fixed size

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.

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