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
}