I am trying to load some images based on smallest width attribute. To do this, I put the images into the following folders:
drawable-sw320dp
drawable-sw360dp
drawable-sw480dp
drawable-sw600dp
drawable-sw720dp
But I wonder what the smallest size should be to avoid crashes at runtime. What happens if a device (if exists) with smallest width 240 runs my app (it probably crashes at runtime)? Can I make a folder named
drawable-sw0dp
and put some images in it so that if smallest width attribute is between 0 and 320, those images are loaded?
I can do this programmatically like the following, but I wonder whether I can do this without needing to write any code.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
float smallestWidth = Math.min(widthDp, heightDp);
if (smallestWidth < 320) {
//Load necessary images
}
else if (smallestWidth >= 320 && smallestWidth < 360) {
//Load necessary images
}else if (smallestWidth >= 360 && smallestWidth < 480) {
//Load necessary images
}else if (smallestWidth >= 480 && smallestWidth < 600) {
//Load necessary images
}else if (smallestWidth >= 720) {
//Load necessary images
}
You should categorise the images on the basis of screen densities. Not on the basis of screen width.
There are few types of screen densities :-
ldpi ~ 120dpi
mdpi ~ 160dpi
hdpi ~ 240dpi
xhdpi ~ 320dpi
xxhdpi ~ 480dpi
xxxhdpi ~ 640 dpi
Every device is categorised between them only.
Their folders are like :-
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Images for smallest screen densities in ldpi. Images for largest screen densities in xxxhdpi.
Other things will be managed by android itself. It will load the correct image depending on the screen density
Always remember mdpi is said to be the base line. That is 1:1 ratio, means in mdpi, 1 px = 1dp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With