Reputation: 677
I'm developing an android app with multiple fragment which every one contains multiple different bitmaps from other fragments. Due to the large size of the .apk file providing those bitmaps for every drawable density folder, i've decided to provide a single high resolution bitmap (a single high res bitmap for every different one from every different fragment) allocated in drawable-xxhdpi and scale it down at runtime depending on device screen resolution and/or density.
I'm a bit confused about scaling bitmaps according to Andropid developer guide about Displaying Bitmaps Efficiently I can't understand if this code scale bitmaps to screen resolution in pixels, or screen density, or both.
As i can see in this code:
mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
It creates a 100x100 image thumbnail, so i supose i have to pass screen resolution or screen density width and height in these parameters, but not sure...
How can I match scaled bitmaps to screen configuration?
Thanks to every answer.
Upvotes: 0
Views: 717
Reputation: 16761
First of all, I would highly discourage not using the built in system of Android which automatically loads an image which appears in more than one drawable folder according to the screen size / density. It's true that doing this will increase the size of your APK, but loading a single bitmap and scaling it down / up during runtime will reduce performance in your app, and may very possibly lead to wasteful memory allocation issues and a wide variety of other bugs.
If you insist on scaling bitmaps on your own, then the 1st problem I can see with your method, is that the parameter your passing (100, 100) is the size in pixels. Devices of different density will have a different amount of pixels, and so 100x100 may appear very large on one device, but very small on a device with better density.
The way to go here would be using device-independant-pixels (dip, or dp). Devices with the same screen size will always have the same dp for their width & height. Since this method you specified requires pixels, then a good way to convert between dp and pixels is shown here
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, amountInDp, r.getDisplayMetrics());
Upvotes: 1