Issue
I've just noticed while checking some decoding times of some resource images,
that the BitmapFactory.decodeResource()
function is about 3-6 times slower, when I move the resource image to the drawable
folder.
Question:
Does anybody know why this happens? Are the images scaled even if they are in the drawable
folder? Is it possible to prevent this without storing a copy of the image in each folder?
Details:
I used the following code just to check the decoding times of an image.
for(int i = 0; i < 5; i++){
long time = System.currentTimeMillis();
BitmapFactory.decodeResource(getResources(),R.drawable.welcome_01);
Log.i(TAG, "Time: " + (System.currentTimeMillis() - time));
}
Bitmap Size: 774 x 1280
Device: Nexus 6 (which uses the drawable-xxxhdpi folder if the resource is available)
Test results:
If the image is in the drawable
folder these are the decoding times:
08-03 09:18:01.072: I/MainActivity(26242): Time: 298
08-03 09:18:01.352: I/MainActivity(26242): Time: 280
08-03 09:18:01.656: I/MainActivity(26242): Time: 304
08-03 09:18:01.929: I/MainActivity(26242): Time: 272
08-03 09:18:02.263: I/MainActivity(26242): Time: 334
If they are in the drawable-xxxhdpi
folder these are the results:
08-03 09:19:49.733: I/MainActivity(26456): Time: 54
08-03 09:19:49.786: I/MainActivity(26456): Time: 53
08-03 09:19:49.841: I/MainActivity(26456): Time: 54
08-03 09:19:49.905: I/MainActivity(26456): Time: 64
08-03 09:19:49.966: I/MainActivity(26456): Time: 61
Solution
I found a solution for my Problem.
It might help someone else.
Solution:
Putting the image into drawable-nodpi
folder prevents the image to be scaled.
Additional Infos:
In every other folder the images are scaled in this case,
even in the drawable
folder.
I've checked the bitmap sizes after decoding the resource inside the different folders.
original size 774x1280
drawable folder: Bitmap: 2709x4480
drawable-ldpi : Bitmap: 3612x5973
drawable-mdpi: Bitmap: 2709x4480
drawable-hdpi: Bitmap: 1806x2987
drawable-xhdpi: Bitmap: 1355x2240
drawable-xxhdpi: Bitmap: 903x1493
drawable-xxxhdpi: Bitmap: 677x1120
drawable-nodpi: Bitmap: 774x1280
Thanks to Joey Chong I found the official answer to the problem. On the android developers page you can read the following lines:
If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density. The system assumes that default resources (those from a directory without configuration qualifiers) are designed for the baseline screen density (mdpi), unless they are loaded from a density-specific resource directory. Pre-scaling is, thus, what the system does when resizing a bitmap to the appropriate size for the current screen density.
In short - If you don't provide a density, android handles them as mdpi.
Answered By - daemmie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.