Issue
I understand that android choices drawables based on their location (-ldpi, -mdpi and so on). How should I organize my images, if I have two sets of drawables, one for mdpi and ldpi and another for hdpi and xhdpi?
Solution
For now, I didn't come across any possibility to group dpi folders. But you still got choices:
Dummy drawable:
Put your images into your normal /drawable
folder and name them like image1_low.png
and image1_high.png
. Now put a "dummy drawable" file image1_dummy.xml
into your /drawable-ldpi
and /drawable-mdpi
folders, containing something like this:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/image1_low" />
The same goes for the /drawable-hdpi
and /drawable-xhdpi
folders, but now with @drawable/image1_high
instead of @drawable/image1_low
.
Now you can refer to the name of the dummy, e.g. by
getResources().getDrawable(R.drawable.image1_dummy);
in your code to get the desired drawable.
Programmatical:
You're not restricted to using folders, there is also a programmatical approach (credits to https://stackoverflow.com/a/5323437/1140682):
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_XHIGH:
// assign your high-res version
break;
case DisplayMetrics.DENSITY_HIGH:
// assign your high-res version
break;
default:
// assign your low-res version
break;
}
Answered By - saschoar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.