Hi, I have exactly the same problem. Did you find a solution for this random DisplayMetrics restet to defaults? Or even an alternative solution to get the same effect?
On 27 Okt., 10:12, arberg <[email protected]> wrote: > I just noticed that the effect of updateConfig() seems to stick if > applied from onCreateDialog, after a dialog has been displayed (and > the updateConfig-code has been executed). After changing the > DisplayMetrics density from actitvity.onCreateDialog, new activities > also gets the custom density. To pin it out: > > 1) If I start game, apply custom-density to DisplayMetrics in > activity.onCreate then activity mainview has custom density > 2) if i start a normal preference activity it has normal density, if I > inflate a subview (by clicking a button), it has normal density > 3) if I display a dialog, and apply custom-density to DisplayMetrics > in onCreateDialog, then that dialog gets custom density > 4) and following starts of preference activity gets custom density, as > well as inflated subviews in main activity. > > It seems like at bug, that the state of the resources displaymetrics > changes, sometimes maintaining custom density, sometimes reverting to > original density. > > Alex > > On Oct 26, 11:02 pm, arberg <[email protected]> wrote: > > > > > > > > > I would like tochangethe 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 aDisplayMetrics > > from the resources, andchangeits 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(); > > DisplayMetricsdm = 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 thedisplayMetricsreturns 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 Localechangeby invoking the > > resources.updateConfiguration, then the Locale-changewould 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 givenDisplayMetrics, > > 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 tochangeautoscale 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

