I would like to change the density configuration so that I can force a
certain size. Specifically I want all 720p screens including xhigh-
density phones AND large medium-density tablets to use the same
bitmap, and to have the same text size relative to the screen
resolution. The result will be that my game looks the same on tablets
and on phones, without having to rework all my layouts and dimensions
to large and xlarge screens, and without having to place the same high-
res bitmaps in both the xhdpi folder and the xlarge (or sw720dp)-
folder.
I have found one method of doing this. If I obtain a DisplayMetrics
from the resources, and change its values then the resources decoded
and inflated after the modification are decoded based on the new
density value. Thus I can run the updateConfig() from early in my
activity onCreate and then the activity is inflated with my custom
density-value.
private void updateConfig() {
Resources resources = getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
final int minDimension = Math.min(dm.widthPixels,
dm.heightPixels);
float densityDevice = minDimension / 320f *
DisplayMetrics.DENSITY_MEDIUM;
// Avoid scaling bitmaps in resources further by setting
density to
// match the one of the levels exactly
if (densityDevice > DisplayMetrics.DENSITY_XHIGH) {
densityDevice = DisplayMetrics.DENSITY_XHIGH;
} else if (densityDevice > DisplayMetrics.DENSITY_HIGH) {
densityDevice = DisplayMetrics.DENSITY_HIGH;
} else if (densityDevice > DisplayMetrics.DENSITY_MEDIUM) {
densityDevice = DisplayMetrics.DENSITY_MEDIUM;
} else {
densityDevice = DisplayMetrics.DENSITY_LOW;
}
dm.scaledDensity = dm.density = densityDevice / (float)
DisplayMetrics.DENSITY_DEFAULT;
dm.densityDpi = (int) densityDevice;
dm.xdpi = densityDevice;
dm.ydpi = densityDevice;
// calling resources.updateConfiguration here has no effect (it just
sets the the same dm-values again in the dm-object), but maybe its
more future proof?
resources.updateConfiguration(null, dm);
}
Updating the density with my updateConfig() method early in
activity.onCreate affects all the resources I decode in onCreate, but
when onCreate returns the displayMetrics returns to the default
values. So if I do a post to UI thread and inflates some more
resources then I have to run updateConfig() again to apply my custom
density. Had I done a Locale change by invoking the
resources.updateConfiguration, then the Locale-change would have taken
effect for the remainder of the activity.
I can see from the implemenation of resources.updateConfiguration (in
Android-10) than it doesn't do much based on the given DisplayMetrics,
it doesn't even flush the bitmap cache, which could cause trouble if I
had already decoded a bitmap with the original density information.
I would like a way to change autoscale by density of the decoded
resources, for the remainder of my activity execution and preferable
even for the entire application if possible.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en