Reputation: 551
I just got the new Nexus 7 which pulls images from the drawable-xhdpi folder because it is 1920x1200. The Nexus 10 also pulls from the drawable-xhdpi folder. The same image looks too big on the Nexus 7 as a result (too big by 150% to be exact). Is there any way to specify a resourse (drawable) folder to use for a specific device?
It sounds like I should not be trying to over-ride the default drawable buckets since there are so many screen sizes and it could hinder me when I attempt to support even more devices. So then what is the best way to handle this problem? The below images illustrates my problem exactly. The play button gets scaled down from the Nexus 10 to the new Nexus 7 while the bearded guy gets scaled up so that it remains as the same physical size on both devices. My images are currently doing what the bearded guy image is doing but I would like it to do what the play/connect buttons do which is scale down on the New Nexus 7. (top image = New Nexus 7 bottom image = Nexus 10)
Upvotes: 0
Views: 1421
Reputation: 116322
You can't set a drawable folder for a specific device, and it's also impractical, since there are too many devices out there (see here for example).
However, if you are unhappy with the way android choose the buckets of which file to use for each device configuration, you can use your own algorithm. there are 2 options to where to put the files in this case:
put the image files in res/drawable-nodpi (or res/raw) . for each image add a suffix/prefix to show which configuration it's used for.
put the image files in assets . this is a more flexible method, since you can even create sub folders and any file name you wish.
in both methods, you should also use your own image-sampling method , depending on your own rules.
Upvotes: 1
Reputation: 551
In order to keep using the drawable resource folders, I had to do the following to differentiate the Nexus 7 2013 and the Nexus 10 since they both would use the xhdpi folder by default:
For the Nexus 7 I ended up using: drawable-sw600dp-xhdpi
For the Nexus 10 I ended up using: drawable-sw720dp-xhdpi
I had 2 sets of assets scaled differently in each folder.
Upvotes: 2
Reputation: 701
You can use the following code to get the size and then scale the image to the required size based on the size of the display.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenDensity = metrics.densityDpi;
Upvotes: 0